473,320 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

datasets, datagrids, and datatables

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[i]["Customer"] = nodeC[i].InnerText;
tCat.Rows[i]["Engine"] = nodeE[i].InnerText;
tCat.Rows[i]["Cubic Inches"] = nodeCI[i].InnerText;
tCat.Rows[i]["Date"] = nodeD[i].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
Nov 17 '05 #1
2 3367
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" wrote:
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[i]["Customer"] = nodeC[i].InnerText;
tCat.Rows[i]["Engine"] = nodeE[i].InnerText;
tCat.Rows[i]["Cubic Inches"] = nodeCI[i].InnerText;
tCat.Rows[i]["Date"] = nodeD[i].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

Nov 17 '05 #2
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);

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: William Ryan | last post by:
At the risk of sounding like a Big 5 consultant, "It depends". 1) Strongly typed datasets rock, they are faster than untyped, use intellisense... but your reason for wanting to use them is...
45
by: cody | last post by:
I've seen an Introduction on ADO.NET with its Datasets on .NET TV and Iam now wondering how it is realized/used in real world applications. I don't believe that one would create a dataset and add...
2
by: malcolm | last post by:
Hello, We have a robust (.NET 1.1 c# winforms) client-server application that utilizes many typed DataSets, typed DataTables and typed DataRows. Our application is a series of windows and popup...
4
by: Dave | last post by:
(My apologies for posting this on two forums. I have just found out the other one was the incorrect location) I am writing a VB.NET 2003 web application to operate on my company's intranet. It...
3
by: cj | last post by:
I've used datatables and datasets before. Datasets being able to hold more than one table and datatables being only one table. My mind keeps coming up with recordsets. I can't remember how they...
0
by: S.Tedeschi | last post by:
Hi all; as posted some days ago, I'm converting an on-line app; I used to heavily rely on strongly-typed DataSets directly dropped onto pages, and so viewed by code(-behind) as well. In the next...
0
by: Teo | last post by:
Hey guys!! I am looking at a user friendly tutorial on how to use Dataadapters, datatables and datasets to fill datagrids, etc. I am confused on using them a lot. Now I mostly use Datareaders and...
12
by: BillE | last post by:
I'm trying to decide if it is better to use typed datasets or business objects, so I would appreciate any thoughts from someone with more experience. When I use a business object to populate a...
1
by: Mark Baldwin | last post by:
Steven Thanks for your reply, however the typed datasets are defined in the web service and there seems to way to open the partial class code window - double clicking on the design surface does...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.