MertYılmaz
MertYılmaz

Reputation: 55

asp.net gridview set null to datasource

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

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 ?

Upvotes: 0

Views: 1042

Answers (1)

Andrei
Andrei

Reputation: 56688

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.

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.

Upvotes: 1

Related Questions