Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using DataGridView newbie question 1

Status
Not open for further replies.

Longleon

Programmer
Joined
Aug 10, 2005
Messages
4
Location
US
I'm just beginning to learn this language. I'm reading books, internet articles and docs and attempting to do most of this on my own. I apologize if this is not the place to ask newbie questions.

I'm writing an app using the VS beta and I open a form and pass an ID into the constructor from another form. I have several queries running in the new form, using that ID but I want the dataGridView to display some values from a table, filtered by the ID I've passed in. I've been relying on the wizards because I can't seem to find any good coding examples using this and I just can't get it done. Can anyone point me to a good example or push me in the right direction?
 
1) DataGridView bound to DataTable



DataGridView dgv = new DataGridView();

BindingSource bs = new BindingSource();

DataTable dt = GetDataTableFromSomeWhere(); // Gets the DataTable



bs.DataSource = dt;

dgv.DataSource = bs;



That’s it. You are done! DataGridView is now bound to the DataTable.



Ofcourse you can bind the DataGridView to the DataTable directly and bypass the BindingSource, but BindingSource has certain advantages:

- It exposes properties to Sort the list, Filter the list, etc. which would other wise be a pain to do. (i.e. if you bind the DataGridView to the DataTable directly then to Sort the DataTable you need to know that DataTable is an IListSource which knows the underlying list which is a DataView and a DataView can be sorted, filtered, etc.).

-

 
here is a nice one on msdn

C#]
private void InitializeSortedFilteredBindingSource()
{
// Create the connection string, data adapter and data table.
SqlConnection connectionString =
new SqlConnection("Initial Catalog=Northwind;" +
"Data Source=localhost;Integrated Security=SSPI;");
SqlDataAdapter customersTableAdapter =
new SqlDataAdapter("Select * from Customers", connectionString);
DataTable customerTable = new DataTable();

// Fill the the adapter with the contents of the customer table.
customersTableAdapter.Fill(customerTable);

// Set data source for BindingSource1.
BindingSource1.DataSource = customerTable;

// Filter the items to show contacts who are owners.
BindingSource1.Filter = "ContactTitle='Owner'";

// Sort the items on the company name in descending order.
BindingSource1.Sort = "Country DESC, Address ASC";

// Set the data source for dataGridView1 to BindingSource1.
dataGridView1.DataSource = BindingSource1;

// Autosize the columns for dataGridView1.
this.dataGridView1.AutoSizeColumns(DataGridViewAutoSizeColumnCriteria.Rows);
}

 
That did the trick. If I had it my way I wouldn't mess with any of the wizards in this IDE in the first place.

My Thanks,
Long
 
One last question: The code I wrote is working fine but can anyone tell me why the column won't hide here?

DealRolesTableAdapter.Fill(DealRolesTable);
DealRolesBS.DataSource = DealRolesTable;
this.DealAssignment.DataSource = DealRolesBS;
this.DealAssignment.Columns[0].Visible = false;
 
Check if its has property called Hidden:)
If it has make that true
 
I've isolated the problem to the fact that I'm using a tabbed control. This DGV is on the second tab in my form. I've tried putting the exact same code on a new form the code behaves as expected. I've tried like 10 different events and can't seem to find the right one for hiding this column.

Also, I stuck some code in that would hide a column when I clicked on it and that worked just fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top