472,145 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Clearing a dataGridView for reuse

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

}

}
Dec 29 '05 #1
3 29541
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" wrote:
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());

}

}

Dec 29 '05 #2
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 [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:47**********************************@microsof t.com...
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" wrote:
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());

}

}

Dec 29 '05 #3
On Thu, 29 Dec 2005 17:04:34 -0500, "RSH" <wa*************@yahoo.com>
wrote:
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}
Dec 31 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Charles Prince | last post: by
1 post views Thread by RSH | last post: by
7 posts views Thread by Mitchell S. Honnert | last post: by
8 posts views Thread by cj | last post: by
reply views Thread by NvrBst | last post: by
2 posts views Thread by RobcPettit | last post: by
reply views Thread by enrico via DotNetMonster.com | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.