动态重新排序Gridview中的列

时间:2017-03-15 03:52:03

标签: c# asp.net gridview

我有一个gridview我试图重新排序列,但是我找到了一个找不到对象的错误。 当我没有在" //代码下包含代码来移动列"时代码有效,但是当我包含该代码时,我得到一个未找到的对象错误" s + = txtAD.Text.Trim();"对象引用未设置为实例。它很奇怪,因为无论我切换哪一列,它总是会抛出错误,它无法找到第1列。

有什么想法吗?

我的HTML:

<asp:GridView ID="gridviewtxSLds" ClientIDMode="Static" runat="server" CssClass="gridclassscrolledtx" CellPadding="0" ForeColor="#333333" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="Metric">
            <ItemTemplate>
                <asp:TextBox runat="server" Text='<%# Bind("item") %>'
                    ID="txtctrltype" class="modalpopup2" AutoPostBack="false"></asp:TextBox>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Center" />
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="AD">
            <ItemTemplate>
                <asp:TextBox runat="server" Text='<%# Bind("AD") %>'
                    ID="txtAD" onfocus="blur()" class="txttime" AutoPostBack="false" ReadOnly="false"></asp:TextBox>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Center" />
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="BD">
            <ItemTemplate>
                <asp:TextBox runat="server" Text='<%# Bind("BD") %>'
                    ID="txtBD" onfocus="blur()" class="txttime" AutoPostBack="false" ReadOnly="false"></asp:TextBox>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Center" />
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="CD">
            <ItemTemplate>
                <asp:TextBox runat="server" Text='<%# Bind("CD") %>'
                    ID="txtCD" onfocus="blur()" class="txttime" AutoPostBack="false" ReadOnly="false"></asp:TextBox>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Center" />
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>
代码隐藏中的

代码用于填充gridview

using (var cmdtx2 = new SqlCommand(sqlcmd, conn))
{
    DataTable dv = new DataTable();
    SqlDataAdapter db = new SqlDataAdapter(cmdtx2);

    //code to move columns
    var columnMove = gridviewtxSLds.Columns[3];
    gridviewtxSLds.Columns.RemoveAt(3);
    gridviewtxSLds.Columns.Insert(2, columnMove);

    db.Fill(dv);
    gridviewtxSLds.DataSource = dv;
    gridviewtxSLds.DataBind();
}

更新代码,会引发错误:

protected void Bulk_Insert(object sender, EventArgs e)
{
    var timeinitid = hfptstate.Value;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < gridviewtxSLds.Rows.Count; i++)
    {
        #region textbox declarations
        TextBox txtitem = gridviewtxSLds.Rows[i].FindControl("txtctrltype") as TextBox;
        TextBox txtAD = gridviewtxSLds.Rows[i].FindControl("txtAD") as TextBox;
        TextBox txtBD = gridviewtxSLds.Rows[i].FindControl("txtBD") as TextBox;
        TextBox txtCD = gridviewtxSLds.Rows[i].FindControl("txtCD") as TextBox;
        #endregion textbox declarations

        #region command string
        string s = "update 1874tx set AD";
        s += "='";
        //following line throws the error
        s += txtAD.Text.Trim();
        s += "', ";
        s += "BD";
        s += "='";
        s += txtBD.Text.Trim();
        s += "', ";
        s += "CD";
        s += "='";
        s += txtCD.Text.Trim();
        s += "' where item";
        s += "='";
        s += txtitem.Text.Trim();
        s += "';";
        #endregion
        sb.Append(s.ToString());
        SqlCommand cmd = new SqlCommand(sb.ToString(), con);
        con.Open();
        cmd.ExecuteNonQuery();
    }
}

编辑:在有人评论SQL查询的连接之前,我知道,并且对于这个特定用途它并不重要。

0 个答案:

没有答案