I've tried, but I haven''t been able to find the namespace containing the dgDetails class for the DataGridView control. Can anybody tell me where it is? BTW, this is for C#.

Bob,
What are you referring to, there is no dgDetails class for that component.

What are you trying to get to (DGV elements) and maybe I can help.

// Jerry

JerryShaw:

From the C# tutorial at Programmer's Heaven (http://www.programmersheaven.com/2/Les_CSharp_13_p9):

private void btnLoadData_Click(object sender, System.EventArgs e)
{
    string connectionString = "server=P-III; database=programmersheaven; uid=sa; pwd=;";
    SqlConnection conn = new SqlConnection(connectionString);
    string cmdString = "SELECT * FROM article";
    SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdString, conn);
    DataSet ds = new DataSet();
    dataAdapter.Fill(ds, "article");
    dgDetails.SetDataBinding(ds, "article");
}

When I try this, I get the following error: "The name 'dgDetails' does not exist in the current context"

The dgDetails is a BindingSource.
The DataGridView is most likely set to use this bindingsource so that when the bindingsource sets the binding to the dataset its datamember of most lilkey the first datatable is then displayed in the gridview.

// Jerry

JerryShaw:

Thanks for your reply.

I'm a newbie, so I'm not familiar with BindingSources.

Could you please tell me what I need to include in my code to get the compiler to recognize the dgDetails BindingSource and compile my app?

Bob,

Sorry for the delay, been busy.

Okay, the programmers haven example is using the old DataGrid component that has been replaced with the newer DataGridView component in VS2005 and higher. The old one is available by adding it to the toolbox, but instead of telling you more about that, I will show you how to use the DataGridView.

First drag a DataSet component from the toolbox onto the form.
Name it "ds" for now.
Now drag a DataGridView component onto the form. For now, just ignore its wizard and use the untyped option. Name it "dgDetails".

private void btnLoadData_Click(object sender, System.EventArgs e)
{
    string connectionString = "server=P-III; database=programmersheaven; uid=sa; pwd=;";
    SqlConnection conn = new SqlConnection(connectionString);
    string cmdString = "SELECT * FROM article";
    SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdString, conn);
    //DataSet ds = new DataSet(); 
    dataAdapter.Fill(ds, "article");
    //dgDetails.SetDataBinding(ds, "article");
   dgDetails.DataSource = ds;
   dgDetails.DataMember = ds.Tables[0].TableName;
}

Now if you want to know more about the BindingSource, I will give you a short definition, and you will need to study it further.

The BindingSource can be dropped onto your form, and instead of setting the DataGridView DataSource to the adapter like we did above, set it to the BindingSource in the designer (IDE).
Set the BindingSource.DataSource and DataMember like we did above to the DataSet you dropped (DataMember goes to the table in the dataset which you can manually create in the IDE).

Now the BindingSource offers a few advantages, you can assign multiple components to this BindingSource so that as you move through the records, all of the bound components will automatically update. Example is a bunch of labels, or textboxes that are data bound to a field in the datatable. Also you can add a BindingNavigator which gives you a component to help navigate through the data. Because it is also bound to the BindingSource, as it repositions the records pointer, all of the bound components will reflect the data for that record.

Hope this helps,
Jerry

This article has been dead for over six months. Start a new discussion instead.