Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am dynamically binding the Grid view on button click. But the problem is that if I again press on the button the duplicate data displays.

for ex.
button click
abc xzy
1 1
2 1

again button click
button click
abc xzy abc xzy
1 1 1 1
2 1 2 1

Please help.
C#
DataTable GetBatchDetail = new DataTable();
GetBatchDetail = objAdmin.abc(FromDate, Todate, cityid, Type);

foreach (DataColumn col in GetBatchDetail.Columns)
{
           
  BoundField bField = new BoundField();
  bField.DataField = col.ColumnName;
  bField.HeaderText = col.ColumnName;
  GridView1.Columns.Add(bField);
}
GridView1.DataSource = GetBatchDetail;
//Bind the datatable with the GridView.
GridView1.DataBind();
Posted
Updated 29-Nov-11 23:48pm
v2
Comments
RaisKazi 30-Nov-11 5:49am    
Edited: 1) Formatting 2) Added "pre" tag.

Try to add this code before your code :
C#
//It will clear old DataBindings
dataGridView1.DataBindings.Clear
dataGridView1.Columns.Clear();

I hope it will help you :)
 
Share this answer
 
v2
Add this line in ur code

C#
GridView1.Columns.Clear();



Replace ur code

C#
DataTable GetBatchDetail = new DataTable();
GetBatchDetail = objAdmin.abc(FromDate, Todate, cityid, Type);

 GridView1.Columns.Clear();

foreach (DataColumn col in GetBatchDetail.Columns)
{
           
  BoundField bField = new BoundField();
  bField.DataField = col.ColumnName;
  bField.HeaderText = col.ColumnName;
  GridView1.Columns.Add(bField);
}
GridView1.DataSource = GetBatchDetail;
GridView1.DataBind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900