C# 如何将具有不同表的datatable绑定到已排序的gridview

C# 如何将具有不同表的datatable绑定到已排序的gridview,c#,asp.net,gridview-sorting,C#,Asp.net,Gridview Sorting,我正在创建一个动态应用程序。在这里,我提供了添加、更新、删除、分页和排序选项。我有一个在顶部的下拉列表,我把表名放在选项中。问题是,当我选择一个表并对其进行排序,然后从下拉列表中更改该表时,它会显示一个找不到列A的错误(我在上一个表中通过该列进行了排序) 这是我的密码 protected void gv_Sorting(object sender, GridViewSortEventArgs e) { try { string so

我正在创建一个动态应用程序。在这里,我提供了添加、更新、删除、分页和排序选项。我有一个在顶部的下拉列表,我把表名放在选项中。问题是,当我选择一个表并对其进行排序,然后从下拉列表中更改该表时,它会显示一个找不到列A的错误(我在上一个表中通过该列进行了排序)

这是我的密码

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
受保护的无效gv_排序(对象发送器、GridViewSortEventArgs e)
{
尝试
{
string sortdir=string.Empty;
if(dir==SortDirection.升序)
{
dir=排序方向。下降;
sortdir=“Desc”;
sortImage.ImageUrl=“../Images/asc.gif”;
}
其他的
{
dir=排序方向。升序;
sortdir=“Asc”;
sortImage.ImageUrl=“../Images/desc.gif”;
}
DataView sortedView=新数据视图(dt);
sortString=e.SortExpression+“”+sortdir;
sortedView.Sort=sortString;
添加(“视图”,dt);
添加(“sortString”,sortString);
gv.DataSource=sortedView;
gv.DataBind();
int columnIndex=0;
foreach(gv.HeaderRow.单元格中的DataControlFieldHeaderCell headerCell)
{
if(headerCell.ContainingField.SortExpression==e.SortExpression)
{
columnIndex=gv.HeaderRow.Cells.GetCellIndex(headerCell);
}
}
gv.HeaderRow.Cells[columnIndex].Controls.Add(排序);
}
捕获(例外情况除外)
{
AMP1.Title=“无法获取数据!”;
AMP1.消息=例如消息;
AMP1.MessageDescription=ex.StackTrace;
AMP1.AppMessageType=TEMP.Controls.AplicationMessagePanel.MessageType.Error;
((按钮)AMP1.FindControl(“btnOK”)).CausesValidation=false;
((按钮)AMP1.FindControl(“btnOK”)).Focus();
ModalPopupExtenderException.Show();
}
}
公共void createGrid(数据表dtTable)
{
尝试
{
//设置标志以获取第一个dropdownlist名称
布尔标志=真;
dtTable.Columns.Add(“SrNo”,typeof(String)).SetOrdinal(0);
//在添加新列之前清除所有列
gv.Columns.Clear();
int count=gv.Columns.count-1;
//为网格创建列
对于(int i=0;idt_table.DataSource = grid.ToArray();
dt_table.DataBind();