Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

Clearing a dataGridView for reuse

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RSH - 29 Dec 2005 20:44 GMT
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());

}

}
Peter Bromberg [C# MVP] - 29 Dec 2005 21:46 GMT
Call the Clear method of the DataGridView's Rows Property, which represents
its Collection of DataGridRows.
-Peter
Signature

Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com

> Hi,
>
[quoted text clipped - 53 lines]
>
> }
RSH - 29 Dec 2005 22:04 GMT
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.

> Call the Clear method of the DataGridView's Rows Property, which
> represents
[quoted text clipped - 60 lines]
>>
>> }
mablejune@otismukinfus.com - 31 Dec 2005 00:08 GMT
>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}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.