【问题标题】:how to sort in gridview using template fields如何使用模板字段在gridview中排序
【发布时间】:2009-12-14 21:08:24
【问题描述】:

当您在 aspx 页面中使用 SQL 数据源填充 gridview 时,排序工作正常...

但现在我正在使用模板字段,并且列在代码隐藏中单独填充,并且排序不起作用......

我的代码是

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
                          AutoGenerateColumns="False" 
                           ondatabound="GridView1_DataBound" 
                        onrowdatabound="GridView1_RowDataBound">


               <Columns>
               <asp:TemplateField HeaderText="File Name" ItemStyle-Width="40%"  >
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server"></asp:Label>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="Failure Count" ItemStyle-Width="10%" >
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server"></asp:Label>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"  />
                            </asp:TemplateField>
               </Columns></GridView>

我的代码隐藏是:

    DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = ConfigurationManager.ConnectionStrings["SumooHAgentDBConnectionString"].ConnectionString;
        connection.Open();
        SqlCommand sqlCmd = new SqlCommand("SELECT  FileName,FailureCount from Files where MachineID=@strID , connection);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlCmd.Parameters.AddWithValue("strID", strID);

        sqlDa.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string nameoffiles = dt.Rows[i]["FileName"].ToString();
                buFailureCode.Add(code);
                string count = dt.Rows[i]["BuFailureCount"].ToString();
                buFailureCount.Add(count);
            }

        }
        connection.Close();
    }


 protected void GridView1_DataBound(object sender, EventArgs e)
    {
        if (namesOfFiles.Count != 0)
        {
            for (int i = 0; i < namesOfFiles.Count; i++)
            {
                GridViewRow myRow = GridView1.Rows[i];
                Label Label1 = (Label)myRow.FindControl("Label1");

                Label Label3 = (Label)myRow.FindControl("Label3");
                Label1.Text = namesOfFiles[i].ToString();

                Label3.Text = buFailureCount[i].ToString();

            }}}

【问题讨论】:

    标签: c# asp.net gridview sorting


    【解决方案1】:

    设置排序表达式

    【讨论】:

      【解决方案2】:

      我认为您必须处理 OnSorting 事件并在代码隐藏中自己进行实际排序。 The API documentation has an example.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-08
        • 2023-04-05
        • 2013-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-06
        • 1970-01-01
        相关资源
        最近更新 更多