asp.net gridview将null设置为datasource

时间:2015-04-22 14:47:03

标签: c# asp.net gridview

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>

如果我点击方法下方的搜索按钮

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();
}

如果我第一次点击搜索按钮结果如下

flow_name

1

如果我点击第二次搜索按钮结果如下

flow name  flow_name


 1           1

如果我点击搜索按钮第三次结果如下

flow name  flow_name   flow_name


1           1           1

问题:

当我点击按钮列和行复制一个但是我设置数据源null onclick到搜索按钮但这不起作用。我怎样才能解决这个问题如何防止复制相同的列和行值?

0 个答案:

没有答案