Clearing a dataGridView for reuse

R

RSH

Hi,

I have a situation where I have a datagridview on a form. I am making a
dynamic table editor where users select a database table and then it calls
this function where the data in the datagrid refreshes with the new table.
Everything works good except every susequent table that is selected is
appended to the data grid view. How do I flush out the data from the
previous bind so that only the new table is displayed?

Thanks,
Ron



private void GetData(string selectCommand)

{

try

{


String connectionString = "Data Source=myDS; Integrated Security=SSPI;
Initial Catalog=master";

SqlConnection cnSQL = new SqlConnection(connectionString);

BindingSource bindingSource1 = new BindingSource();

dataGridView1.DataSource = bindingSource1;

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = new SqlCommand(selectCommand, cnSQL);

SqlCommandBuilder builder = new SqlCommandBuilder(da);

table.Clear();

da.Fill(table);

lblRecordCount.Text = "Table: " + strSQLTable + " - " +
table.Rows.Count.ToString() + " rows";

bindingSource1.DataSource = table;

dataGridView1.Columns[0].ReadOnly = true;

}

catch (SqlException e)

{

MessageBox.Show(e.Message.ToString());

}

}
 
G

Guest

Call the Clear method of the DataGridView's Rows Property, which represents
its Collection of DataGridRows.
-Peter
 
R

RSH

That clears only the data...so when I try to use the dataGridView again it
has all the columns from the initial bind. It keeps adding the columns.



Peter Bromberg said:
Call the Clear method of the DataGridView's Rows Property, which
represents
its Collection of DataGridRows.
-Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




RSH said:
Hi,

I have a situation where I have a datagridview on a form. I am making a
dynamic table editor where users select a database table and then it
calls
this function where the data in the datagrid refreshes with the new
table.
Everything works good except every susequent table that is selected is
appended to the data grid view. How do I flush out the data from the
previous bind so that only the new table is displayed?

Thanks,
Ron



private void GetData(string selectCommand)

{

try

{


String connectionString = "Data Source=myDS; Integrated Security=SSPI;
Initial Catalog=master";

SqlConnection cnSQL = new SqlConnection(connectionString);

BindingSource bindingSource1 = new BindingSource();

dataGridView1.DataSource = bindingSource1;

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = new SqlCommand(selectCommand, cnSQL);

SqlCommandBuilder builder = new SqlCommandBuilder(da);

table.Clear();

da.Fill(table);

lblRecordCount.Text = "Table: " + strSQLTable + " - " +
table.Rows.Count.ToString() + " rows";

bindingSource1.DataSource = table;

dataGridView1.Columns[0].ReadOnly = true;

}

catch (SqlException e)

{

MessageBox.Show(e.Message.ToString());

}

}
 
M

mablejune

That clears only the data...so when I try to use the dataGridView again it
has all the columns from the initial bind. It keeps adding the columns.

yourDataGridView.DataSource = null;


{snip}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top