datasets, datagrids, and datatables

G

Guest

Hi,
Ok, I'm on my own in learning c# for work here and have come pretty far, but
I'm confused by all the options available for datagrids. I have a private
datagrid (dg), private dataset (ds), and private datatable (tCat) all
declared at the very beginning. When I click on the tabpage that contains
the all these, I first MakeDataSet (below), then dg.SetDataBinding(ds,
"Catalog"). Bear with me, I want to make sure I'm doing things right and
would rather give too much info than not enough.

MakeDataSet()
{
// Create a DataSet.
ds = new DataSet("ds");
string catalog = @"C:\Catalog.xml";
XmlDocument xml = new XmlDocument();
xml.Load(catalog);

DataTable tCat = new DataTable("Catalog");

DataColumn cCust = new DataColumn("Customer");
DataColumn cEng = new DataColumn("Engine");
DataColumn cCI = new DataColumn("Cubic Inches");
DataColumn cDate = new DataColumn("Date");
tCat.Columns.Add(cCust);
tCat.Columns.Add(cEng);
tCat.Columns.Add(cCI);
tCat.Columns.Add(cDate);

// Add the tables to the DataSet.
ds.Tables.Add(tCat);

XmlNodeList nodeC = xml.GetElementsByTagName("Cust");
XmlNodeList nodeE = xml.GetElementsByTagName("Engine");
XmlNodeList nodeCI = xml.GetElementsByTagName("CI");
XmlNodeList nodeD = xml.GetElementsByTagName("Date");

// Add as many rows as I have customers
DataRow newRow;
for(int i = 0; i < nodeC.Count; i++)
{
newRow = tCat.NewRow();
// Add the row to the Customers table.
tCat.Rows.Add(newRow);
}
// Give each customer a distinct name.
for (int i = 0; i < nodeC.Count; i++)
{
tCat.Rows["Customer"] = nodeC.InnerText;
tCat.Rows["Engine"] = nodeE.InnerText;
tCat.Rows["Cubic Inches"] = nodeCI.InnerText;
tCat.Rows["Date"] = nodeD.InnerText;
}

AddCustomDataTableStyle();
}

AddCustomDataTableStyle()
{
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Catalog";

DataGridColumnStyle cCust = new DataGridTextBoxColumn();
cCust.MappingName = "Customer";
cCust.HeaderText = "Customer";
cCust.Width = 210;
ts1.GridColumnStyles.Add(cCust);
... same thing for each column Eng, CI, Date ...

dg.TableStyles.Add(ts1);
}

Now, when a mouseup event occurs in the datagrid, I have:

private void dgMouse(object sender, MouseEventArgs e)
{
// Get the DataGrid by casting sender.
DataGrid Grid = (DataGrid)sender;
DataGrid.HitTestInfo HitInfo = Grid.HitTest(e.X, e.Y);
if(HitInfo.Type !=
DataGrid.HitTestType.ColumnHeader){this.dg.Select(HitInfo.Row);}

DataGrid.HitTestInfo hitTest = dg.HitTest(e.X, e.Y);

if(hitTest.Type == DataGrid.HitTestType.ColumnHeader)
{
// Get the CurrencyManager for the bound DataTable
CurrencyManager cm = (CurrencyManager)this.dg.BindingContext[this.tCat];

// Get the current DataRowView
DataRowView rv = (DataRowView)cm.Current;

// Remember the currently selected row ID
this.currentlySelectedRowId = (int)rv["key"];
}
}
(and here's where the dg.SetDataBinding(ds, "Catalog") comes in from above.
)
First, I'm confused because at the very top, I get the squiggly blue line
underlining the private tCat declaration which says that tCat is never
assigned to and will always have it's default value null. But ds and dg are
fine. I appear to assign tCat to ds under the MakeDataSet()?
Now when I try to run the program, the dgMouse() is fine when I click some
cell (the selected row turns a different color). But when a column header is
clicked, I get the green error arrow at the line where I have the
CurrencyManager cm under dgMouse. The error is
'System.ArgumentNullException': Value cannot be null. I'm assuming this has
something to do with the null tCat?
Anyway, any help in sorting this stuff out would be great. (Do I have to
have a datadet in order to have a datatable?)
Thanks again!
Mel
 
G

Guest

Ok, nevermind, I found the error that caused the null table. As usual, it
was a pretty simple solution (took out the DataTable before the dt = xxxxx
under MakeDataSet()).


melanieab said:
Hi,
Ok, I'm on my own in learning c# for work here and have come pretty far, but
I'm confused by all the options available for datagrids. I have a private
datagrid (dg), private dataset (ds), and private datatable (tCat) all
declared at the very beginning. When I click on the tabpage that contains
the all these, I first MakeDataSet (below), then dg.SetDataBinding(ds,
"Catalog"). Bear with me, I want to make sure I'm doing things right and
would rather give too much info than not enough.

MakeDataSet()
{
// Create a DataSet.
ds = new DataSet("ds");
string catalog = @"C:\Catalog.xml";
XmlDocument xml = new XmlDocument();
xml.Load(catalog);

DataTable tCat = new DataTable("Catalog");

DataColumn cCust = new DataColumn("Customer");
DataColumn cEng = new DataColumn("Engine");
DataColumn cCI = new DataColumn("Cubic Inches");
DataColumn cDate = new DataColumn("Date");
tCat.Columns.Add(cCust);
tCat.Columns.Add(cEng);
tCat.Columns.Add(cCI);
tCat.Columns.Add(cDate);

// Add the tables to the DataSet.
ds.Tables.Add(tCat);

XmlNodeList nodeC = xml.GetElementsByTagName("Cust");
XmlNodeList nodeE = xml.GetElementsByTagName("Engine");
XmlNodeList nodeCI = xml.GetElementsByTagName("CI");
XmlNodeList nodeD = xml.GetElementsByTagName("Date");

// Add as many rows as I have customers
DataRow newRow;
for(int i = 0; i < nodeC.Count; i++)
{
newRow = tCat.NewRow();
// Add the row to the Customers table.
tCat.Rows.Add(newRow);
}
// Give each customer a distinct name.
for (int i = 0; i < nodeC.Count; i++)
{
tCat.Rows["Customer"] = nodeC.InnerText;
tCat.Rows["Engine"] = nodeE.InnerText;
tCat.Rows["Cubic Inches"] = nodeCI.InnerText;
tCat.Rows["Date"] = nodeD.InnerText;
}

AddCustomDataTableStyle();
}

AddCustomDataTableStyle()
{
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Catalog";

DataGridColumnStyle cCust = new DataGridTextBoxColumn();
cCust.MappingName = "Customer";
cCust.HeaderText = "Customer";
cCust.Width = 210;
ts1.GridColumnStyles.Add(cCust);
... same thing for each column Eng, CI, Date ...

dg.TableStyles.Add(ts1);
}

Now, when a mouseup event occurs in the datagrid, I have:

private void dgMouse(object sender, MouseEventArgs e)
{
// Get the DataGrid by casting sender.
DataGrid Grid = (DataGrid)sender;
DataGrid.HitTestInfo HitInfo = Grid.HitTest(e.X, e.Y);
if(HitInfo.Type !=
DataGrid.HitTestType.ColumnHeader){this.dg.Select(HitInfo.Row);}

DataGrid.HitTestInfo hitTest = dg.HitTest(e.X, e.Y);

if(hitTest.Type == DataGrid.HitTestType.ColumnHeader)
{
// Get the CurrencyManager for the bound DataTable
CurrencyManager cm = (CurrencyManager)this.dg.BindingContext[this.tCat];

// Get the current DataRowView
DataRowView rv = (DataRowView)cm.Current;

// Remember the currently selected row ID
this.currentlySelectedRowId = (int)rv["key"];
}
}
(and here's where the dg.SetDataBinding(ds, "Catalog") comes in from above.
)
First, I'm confused because at the very top, I get the squiggly blue line
underlining the private tCat declaration which says that tCat is never
assigned to and will always have it's default value null. But ds and dg are
fine. I appear to assign tCat to ds under the MakeDataSet()?
Now when I try to run the program, the dgMouse() is fine when I click some
cell (the selected row turns a different color). But when a column header is
clicked, I get the green error arrow at the line where I have the
CurrencyManager cm under dgMouse. The error is
'System.ArgumentNullException': Value cannot be null. I'm assuming this has
something to do with the null tCat?
Anyway, any help in sorting this stuff out would be great. (Do I have to
have a datadet in order to have a datatable?)
Thanks again!
Mel
 
D

DBC User

I see in MakeDataSet method you create a dataset. When are you showing
the datagrid to on the form? Right before you show the form/datagrid,
bind the dataset to the datagrid.
Also you can have multiple tables in a single dataset, so you need to
specify which table you want to bind to datagrid. Something like the
following
this.grid1.SetDataBinding(ds, ds.Tables[0].TableName);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top