gridview不会根据不同的查询显示数据

时间:2015-02-19 22:58:17

标签: c# asp.net gridview datagridview



this is the geidview code 
 < asp: GridView ID = "GridView1"
 runat = "server"
 AutoGenerateColumns = "False"
 DataKeyNames = "studentID,startDate"
 DataSourceID = "SqlDataSource1"
 Width = "1230px"
 BackColor = "White"
 BorderColor = "#CCCCCC"
 BorderStyle = "None"
 BorderWidth = "1px"
 CellPadding = "4"
 ForeColor = "Black"
 OnRowCommand = "GridView1_RowCommand"
 OnRowDataBound = "OnRowDataBound"
 OnPreRender = "gridView_PreRender" >
   < Columns >
   < asp: BoundField DataField = "firstName"
 HeaderText = "First Name"
 SortExpression = "firstName" / >
   < asp: BoundField DataField = "lastName"
 HeaderText = "Last Name"
 SortExpression = "lastName" / >
   < asp: BoundField DataField = "studentID"
 HeaderText = "Student ID"
 ReadOnly = "True"
 SortExpression = "studentID" / >
   < asp: BoundField DataField = "startDate"
 HeaderText = "Start Date"
 SortExpression = "startDate"
 dataformatstring = "{0:yyyy-MM-dd}" / >
   < asp: BoundField DataField = "endDate"
 HeaderText = "End Date"
 SortExpression = "endDate"
 dataformatstring = "{0:yyyy-MM-dd}" / >
   < asp: BoundField DataField = "trainingPlace"
 HeaderText = "Training Place"
 SortExpression = "trainingPlace" / >
   < asp: BoundField DataField = "absenceDays"
 HeaderText = "Absence Days"
 SortExpression = "absenceDays" / >
   < asp: BoundField DataField = "evaluation"
 HeaderText = "Evaluation"
 SortExpression = "evaluation" / >
   < asp: BoundField DataField = "comment"
 HeaderText = "Comment"
 SortExpression = "comment" / >
   < asp: ButtonField ButtonType = "Image"
 CommandName = "redirect_edit"
 HeaderText = "Edit"
 ImageUrl = "~/images/table_Edit.png"
 Text = "Edit"
 ShowHeader = "True" / >
   < asp: ButtonField ButtonType = "Image"
 CommandName = "redirect_delete"
 HeaderText = "Delete"
 ImageUrl = "~/images/table_delete.png"
 Text = "Delete" / >
   < asp: ButtonField ButtonType = "Image"
 CommandName = "sendEmail"
 HeaderText = "Certificate"
 ImageUrl = "~/images/table_certificate.png"
 Text = "Certificate"
 ItemStyle - HorizontalAlign = "Center" / >

   < /Columns>
                   <FooterStyle BackColor="#CCCC99" ForeColor="Black" / >
   < HeaderStyle BackColor = "#4DA3B0"
 Font - Bold = "True"
 ForeColor = "White" / >
   < PagerStyle BackColor = "White"
 ForeColor = "Black"
 HorizontalAlign = "Right" / >
   < SelectedRowStyle BackColor = "#CC3333"
 Font - Bold = "True"
 ForeColor = "White" / >
   < SortedAscendingCellStyle BackColor = "#F7F7F7" / >
   < SortedAscendingHeaderStyle BackColor = "#4B4B4B" / >
   < SortedDescendingCellStyle BackColor = "#E5E5E5" / >
   < SortedDescendingHeaderStyle BackColor = "#242121" / >
   < /asp:GridView>
               <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=WESAM\SQLEXPRESS;Initial Catalog=CAMSIS;Integrated Security=True" ProviderName="System.Data.SqlClient"></asp: SqlDataSource >
   < /td>
&#13;
&#13;
&#13;

我正在根据所选学生ID或下拉列表中所选项目编写网格视图更改的代码。话虽如此,我的代码并没有正常工作。我已经尝试了一切,但没有运气。似乎gridview显示页面加载中的第一个查询并忽略其他所有内容。 下面是我的代码。非常感谢您的帮助:)

&#13;
&#13;
  SqlConnection myCon = new SqlConnection(@"Data Source=*****;Initial Catalog=CAMSIS;Integrated Security=True");
  string grade; //This variable is used to distinct the failed students among the passed ones & to hide the certificate button
  int department_selected;
  protected void Page_Load(object sender, EventArgs e) {
    idTextBox.Text = "";
    DepDropDownList.SelectedIndex = 0;
    if (!Page.IsPostBack) //To display the user's profile information once the page loaded
    {
      myCon.Open();

      SqlDataAdapter daTa = new SqlDataAdapter(" select depName from Department", myCon);
      // To copy the department names into an array
      DataTable dt1 = new DataTable();
      daTa.Fill(dt1);
      // Foreach loop to terate through the department names array and add the names to the department dropdown list
      foreach(DataRow row in dt1.Rows) {

        DepDropDownList.Items.Add(row["depName"].ToString());
      }

      SqlDataSource1.SelectCommand = "SELECT Intern.firstName, Intern.lastName, Rotation.studentID, Rotation.startDate, Rotation.endDate, Rotation.trainingPlace, Rotation.absenceDays, Rotation.evaluation, Rotation.comment FROM Intern INNER JOIN Rotation ON Intern.studentID = Rotation.studentID";
      myCon.Close();
    }
  }



  protected void Button2_Click(object sender, EventArgs e) {
    myCon.Open();
    if (DepDropDownList.SelectedIndex == 0 && idTextBox.Text == "") {
      SqlDataSource1.SelectCommand = "SELECT Intern.firstName, Intern.lastName, Rotation.studentID, Rotation.startDate, Rotation.endDate, Rotation.trainingPlace, Rotation.absenceDays, Rotation.evaluation, Rotation.comment FROM Intern INNER JOIN Rotation ON Intern.studentID = Rotation.studentID inner join department on intern.depNumber=Department.depNumber";
      Response.Write("<script language='javascript'>window.alert('1');window.location='deanInternshipSchedule.aspx';</script>");

    } else if (!string.IsNullOrEmpty(idTextBox.Text)) {
      //            SqlDataSource1.SelectCommand = "SELECT Intern.firstName, Intern.lastName, Rotation.studentID, Rotation.startDate, Rotation.endDate, Rotation.trainingPlace, Rotation.absenceDays, Rotation.evaluation, Rotation.comment FROM Intern INNER JOIN Rotation ON Intern.studentID = Rotation.studentID inner join department on intern.depNumber=Department.depNumber where Intern.studentID=" + idTextBox.Text.ToString().Trim() + "";
      Response.Write("<script language='javascript'>window.alert('2');window.location='deanInternshipSchedule.aspx';</script>");

    } else if ((DepDropDownList.SelectedIndex != 0) && (string.IsNullOrEmpty(idTextBox.Text))) {
      Response.Write("<script language='javascript'>window.alert('3');window.location='deanInternshipSchedule.aspx';</script>");

      //SqlDataSource1.SelectCommand = "SELECT Intern.firstName, Intern.lastName, Rotation.studentID, Rotation.startDate, Rotation.endDate, Rotation.trainingPlace, Rotation.absenceDays, Rotation.evaluation, Rotation.comment FROM Intern INNER JOIN Rotation ON Intern.studentID = Rotation.studentID inner join department on intern.depNumber=Department.depNumber WHERE Department.depName='" + DepDropDownList.SelectedValue.ToString() + "'";
    }
    myCon.Close();

  }
&#13;
&#13;
&#13;

0 个答案:

没有答案