简体   繁体   English

asp.net gridview将null设置为datasource

[英]asp.net gridview set null to datasource

Asp.net Html: Asp.net HTML:

<asp:GridView ID="GrdDynamic" runat="server" 
    AllowPaging="true" 
    GridLines="None" 
    PageSize="20" 
    CellSpacing="0" 
    AlternatingItemStyle-CssClass="rowb renkliSatir" 
    CssClass="tumislerimtable" 
    AllowSorting="true"
    AutoGenerateColumns="false">
    <HeaderStyle CssClass="rowa" />
</asp:GridView>

<asp:ImageButton ID="search" runat="server" 
    AlternateText="Search" 
    ImageUrl="../images/search.gif" 
    OnClick="search_Click">
</asp:ImageButton>

If i click to search button below method works 如果我单击下面的搜索按钮,方法有效

search_Click method search_Click方法

protected void search_Click(object sender, ImageClickEventArgs e)
{
    DataTable NullDataTable = new DataTable();
    NullDataTable = null;
    GrdDynamic.DataSource = NullDataTable; // I'm trying to clear gridview datasource however this is not working for me
    GrdDynamic.DataBind();

    //After that i bind datasource as below programatically

    DataTable dt = new DataTable();
    DataRow drow = dt.NewRow();

    foreach (var project in MultiComboBox.CheckedItems.ToList())
    {
        DataSet ds = procedure.getProjectByID(type.SelectedValue, startDate, endDate);

        if (ds.Tables[0].Rows.Count > 0)
        {
            DataColumn dcol = new DataColumn(ds.Tables[0].Rows[0 ["flow_name"].ToString(), typeof(System.Int32));
            dt.Columns.Add(dcol);

            drow[ds.Tables[0].Rows[0]["flow_name"].ToString()] = ds.Tables[0].Rows[0]["Sayi"].ToString();
        }
    }

    dt.Rows.Add(drow);

    foreach (DataColumn col in dt.Columns)
    {
        BoundField bfield = new BoundField();
        bfield.DataField = col.ColumnName;
        bfield.HeaderText = col.ColumnName;
        GrdDynamic.Columns.Add(bfield);
    }

    GrdDynamic.DataSource = dt;
    GrdDynamic.DataBind();
}

If i click to Search Button first time result as below 如果我第一次单击搜索按钮, 结果如下

flow_name

1

If i click to Search button second time result as below 如果我单击搜索按钮第二次结果如下

flow name  flow_name


 1           1

If i click to Search button third time result as below 如果我单击搜索按钮第三次结果如下

flow name  flow_name   flow_name


1           1           1

Question: 题:

When i click button column and row copies one more however i set datasource null onclick to search button but this is not working.How can i solve this problem how can i prevent copy same column and row values ? 当我单击按钮时,列和行会再复制一次,但是我将datasource null设置为onclick来搜索按钮,但这不起作用。如何解决此问题,如何防止复制相同的列和行值?

Note that you data binding trick is clearing up only rows, not columns. 请注意,您的数据绑定技巧只是清除行,而不清除列。 And for rows it does not matter because you do one more data bind in the end of click handler, which fills GridView with data anew. 对于行而言,这无关紧要,因为在单击处理程序的末尾再进行一次数据绑定,这将为GridView重新填充数据。

As for columns, you run this every button click 至于列,您每单击一次按钮就运行一次

foreach (DataColumn col in dt.Columns)

effectively adding new columns every time. 每次都有效地添加新列。 You should only run this once, and on subsequent clicks make sure you do not add already added columns. 您应该只运行一次,并且在随后的单击中确保不添加已添加的列。 Or better yet try to define list of columns declaratively in the markup and get rid of column-adding code at all. 最好还是尝试在标记中声明性地定义列列表,并完全摆脱添加列的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM