By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Clearing a dataGridView for reuse

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.