2017-04-13 17 views
0

我正在創建一個動態應用程序。我在哪裏添加,更新,刪除,分頁和排序選項。我有一個頂級下拉列表,我將表名放在選項中。問題是,當我選擇一個表並對其進行排序並從下拉列表中更改表時,它顯示無法找到A列的錯誤(由此我在上表中完成了排序。)如何將具有不同表格的數據表綁定到已排序的gridview中

這是我的代碼。

protected void gv_Sorting(object sender, GridViewSortEventArgs e) 
    { 
     try 
     { 
      string sortdir = string.Empty; 
      if (dir == SortDirection.Ascending) 
      { 
       dir = SortDirection.Descending; 
       sortdir = "Desc"; 
       sortImage.ImageUrl = "../Images/asc.gif"; 
      } 
      else 
      { 
       dir = SortDirection.Ascending; 
       sortdir = "Asc"; 
       sortImage.ImageUrl = "../Images/desc.gif"; 
      } 

      DataView sortedView = new DataView(dt); 
      sortString = e.SortExpression + " " + sortdir; 
      sortedView.Sort = sortString; 
      ViewState.Add("View", dt); 
      ViewState.Add("sortString", sortString); 
      gv.DataSource = sortedView; 
      gv.DataBind(); 
      int columnIndex = 0; 
      foreach (DataControlFieldHeaderCell headerCell in gv.HeaderRow.Cells) 
      { 
       if (headerCell.ContainingField.SortExpression == e.SortExpression) 
       { 
        columnIndex = gv.HeaderRow.Cells.GetCellIndex(headerCell); 
       } 
      } 
      gv.HeaderRow.Cells[columnIndex].Controls.Add(sortImage); 

     } 
     catch (Exception ex) 
     { 
      AMP1.Title = "Unable to Get Data.!"; 
      AMP1.Message = ex.Message; 
      AMP1.MessageDescription = ex.StackTrace; 
      AMP1.AppMessageType = TEMP.Controls.AplicationMessagePanel.MessageType.Error; 
      ((Button)AMP1.FindControl("btnOK")).CausesValidation = false; 
      ((Button)AMP1.FindControl("btnOK")).Focus(); 
      ModalPopupExtenderException.Show(); 
     } 
    } 


public void createGrid(DataTable dtTable) 
    { 
     try 
     { 
      // setting flag to get first dropdownlist name 
      bool flag = true; 

      dtTable.Columns.Add("SrNo", typeof(String)).SetOrdinal(0); 

      //Clearing all the columns before adding new one 
      gv.Columns.Clear(); 
      int count = gv.Columns.Count - 1; 



      // Creating Columns for the grid 
      for (int i = 0; i < dtTable.Columns.Count; i++) 
      { 
       string colName = dtTable.Columns[i].ColumnName; 
       TemplateField lname = new TemplateField(); 

       if (HeaderText(colName) != "") 
       { 
        lname.HeaderText = HeaderText(colName);// +"<font color='red'>*</font>"; 
       } 
       else 
       { 
        lname.HeaderText = colName;// +"<font color='red'>*</font>"; ; 
       } 

       if (colName != "SrNo") 
       { 
        lname.SortExpression = colName; 
       } 
       if (colName != "ID" && colName != "Active" && colName != "SrNo" && colName != "BModelValue_Val") 
       { 
        lname.ItemStyle.CssClass = "tr1"; 
        lname.FooterStyle.CssClass = "tr2"; 
       } 
       else 
       { 
        if (colName == "BModelValue_Val") 
        { 
         lname.ItemStyle.CssClass = "tdBeveWorm"; 
         lname.FooterStyle.CssClass = "tdBeveWorm"; 
        } 
        else 
        { 
         lname.ItemStyle.CssClass = "tr1"; 
         lname.FooterStyle.CssClass = "tr2"; 
        } 
       } 

       if (colName.EndsWith("_Val") || colName == "ID" || colName == "SrNo") 
       { 
        lname.ItemTemplate = new AddTemplateToGrid(AddTemplateToGrid.enumControlType.Label, colName); 

        if (colName.EndsWith("_Val") && flag == true) 
        { 
         firstddl = colName; 
         flag = false; 
        } 



        if (colName != "ID" && colName != "SrNo") 
         lname.FooterTemplate = new AddTemplateToGrid("footer", AddTemplateToGrid.enumControlType.DropDownList, colName); 
       } 
       else 
       { 
        if (colName == "Active") 
        { 
         lname.ItemTemplate = new AddTemplateToGrid(AddTemplateToGrid.enumControlType.CheckBox, colName); 
         lname.FooterTemplate = new AddTemplateToGrid("footer", AddTemplateToGrid.enumControlType.CheckBox, colName); 
        } 
        else 
        { 
         lname.ItemTemplate = new AddTemplateToGrid(AddTemplateToGrid.enumControlType.TextBox, colName); 
         lname.FooterTemplate = new AddTemplateToGrid("footer", AddTemplateToGrid.enumControlType.TextBox, colName); 

        } 
       } 

       gv.Columns.Add(lname); 
      } 
      // Command Buttons for Grid 
      TemplateField lname1 = new TemplateField(); 
      lname1.HeaderText = "Action"; 
      lname1.ItemTemplate = new AddTemplateToGrid("Item", AddTemplateToGrid.enumControlType.ImageButton, "Action"); 
      lname1.FooterTemplate = new AddTemplateToGrid("footer", AddTemplateToGrid.enumControlType.ImageButton, "Action"); 
      //lname1.FooterStyle.Width = new Unit("150px"); 
      //lname1.ItemStyle.Width = new Unit("150px"); 
      gv.Columns.Add(lname1); 
      gv.AutoGenerateColumns = false; 

      // Checking whether page is sorted or not 
      if (ViewState["View"] != null) 
      { 
       DataView dv1 = new DataView(dtTable); 
       dv1.Sort = ViewState["sortString"].ToString(); 
       // Binding DataView to the Grid 
       gv.DataSource = dv1; 
      } 
      else 
      { 
       // Binding DataTable to the Grid 
       gv.DataSource = dtTable; 
      } 
      gv.DataBind(); 


      //Checking Page is having Index or not and setting HiddenField Value. 
      if (dtTable.Rows.Count > gv.PageSize) 
       hdfPagindex.Value = "1"; 
      else 
       hdfPagindex.Value = "0"; 
     } 
     catch (Exception ex) 
     { 
      AMP1.Title = "Unable to Get Data.!"; 
      AMP1.Message = ex.Message; 
      AMP1.MessageDescription = ex.StackTrace; 
      AMP1.AppMessageType = TEMP.Controls.AplicationMessagePanel.MessageType.Error; 
      ((Button)AMP1.FindControl("btnOK")).CausesValidation = false; 
      ((Button)AMP1.FindControl("btnOK")).Focus(); 
      ModalPopupExtenderException.Show(); 
     } 
    } 


public void loadTable(string tblName) 
    { 
     // Getting Table records 
     dt = null; 
     dt = bl.BindDataToMaster(tblName); 

     // Creating grid according to DataTable 
     gv.Columns.Clear(); 

     createGrid(dt); 

    } 



protected void ddl_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      ViewState["View"] = null; 
      ViewState["sortString"] = null; 

      tblName = ddl.SelectedItem.Value; 
      gv.PageIndex = 0; 
      loadTable(tblName); 
     } 
     catch (Exception ex) 
     { 
      AMP1.Title = "Unable to Get Data.!"; 
      AMP1.Message = ex.Message; 
      AMP1.MessageDescription = ex.StackTrace; 
      AMP1.AppMessageType = WEIR_BDK.Controls.AplicationMessagePanel.MessageType.Error; 
      ((Button)AMP1.FindControl("btnOK")).CausesValidation = false; 
      ((Button)AMP1.FindControl("btnOK")).Focus(); 
      ModalPopupExtenderException.Show(); 
     } 
    } //DropDownList Index Changed 

回答

0

在ASP.net每個數據元素具有兩個東西,第一個是數據源,這是其中數據元素得到的數據,並且第二個是一個稱爲DataBind方法,這是一個無效的方法,例如: 我們假設我的GridView被命名爲grid,DataTable的名稱爲dt_table,並且我的網格充滿了數據,但是我的數據表不是,所以如果我想填充我的網格視圖,我只需簡單地添加此代碼:

dt_table.DataSource = grid.ToArray(); 
dt_table.DataBind(); 

問候。

相關問題