473,791 Members | 3,154 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SetDataBindi ng(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.xm l";
XmlDocument xml = new XmlDocument();
xml.Load(catalo g);

DataTable tCat = new DataTable("Cata log");

DataColumn cCust = new DataColumn("Cus tomer");
DataColumn cEng = new DataColumn("Eng ine");
DataColumn cCI = new DataColumn("Cub ic Inches");
DataColumn cDate = new DataColumn("Dat e");
tCat.Columns.Ad d(cCust);
tCat.Columns.Ad d(cEng);
tCat.Columns.Ad d(cCI);
tCat.Columns.Ad d(cDate);

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

XmlNodeList nodeC = xml.GetElements ByTagName("Cust ");
XmlNodeList nodeE = xml.GetElements ByTagName("Engi ne");
XmlNodeList nodeCI = xml.GetElements ByTagName("CI") ;
XmlNodeList nodeD = xml.GetElements ByTagName("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(n ewRow);
}
// 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;
}

AddCustomDataTa bleStyle();
}

AddCustomDataTa bleStyle()
{
DataGridTableSt yle ts1 = new DataGridTableSt yle();
ts1.MappingName = "Catalog";

DataGridColumnS tyle cCust = new DataGridTextBox Column();
cCust.MappingNa me = "Customer";
cCust.HeaderTex t = "Customer";
cCust.Width = 210;
ts1.GridColumnS tyles.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)sende r;
DataGrid.HitTes tInfo HitInfo = Grid.HitTest(e. X, e.Y);
if(HitInfo.Type !=
DataGrid.HitTes tType.ColumnHea der){this.dg.Se lect(HitInfo.Ro w);}

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

if(hitTest.Type == DataGrid.HitTes tType.ColumnHea der)
{
// Get the CurrencyManager for the bound DataTable
CurrencyManager cm = (CurrencyManage r)this.dg.Bindi ngContext[this.tCat];

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

// Remember the currently selected row ID
this.currentlyS electedRowId = (int)rv["key"];
}
}
(and here's where the dg.SetDataBindi ng(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.Argumen tNullException' : 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 3389
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.SetDataBindi ng(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.xm l";
XmlDocument xml = new XmlDocument();
xml.Load(catalo g);

DataTable tCat = new DataTable("Cata log");

DataColumn cCust = new DataColumn("Cus tomer");
DataColumn cEng = new DataColumn("Eng ine");
DataColumn cCI = new DataColumn("Cub ic Inches");
DataColumn cDate = new DataColumn("Dat e");
tCat.Columns.Ad d(cCust);
tCat.Columns.Ad d(cEng);
tCat.Columns.Ad d(cCI);
tCat.Columns.Ad d(cDate);

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

XmlNodeList nodeC = xml.GetElements ByTagName("Cust ");
XmlNodeList nodeE = xml.GetElements ByTagName("Engi ne");
XmlNodeList nodeCI = xml.GetElements ByTagName("CI") ;
XmlNodeList nodeD = xml.GetElements ByTagName("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(n ewRow);
}
// 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;
}

AddCustomDataTa bleStyle();
}

AddCustomDataTa bleStyle()
{
DataGridTableSt yle ts1 = new DataGridTableSt yle();
ts1.MappingName = "Catalog";

DataGridColumnS tyle cCust = new DataGridTextBox Column();
cCust.MappingNa me = "Customer";
cCust.HeaderTex t = "Customer";
cCust.Width = 210;
ts1.GridColumnS tyles.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)sende r;
DataGrid.HitTes tInfo HitInfo = Grid.HitTest(e. X, e.Y);
if(HitInfo.Type !=
DataGrid.HitTes tType.ColumnHea der){this.dg.Se lect(HitInfo.Ro w);}

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

if(hitTest.Type == DataGrid.HitTes tType.ColumnHea der)
{
// Get the CurrencyManager for the bound DataTable
CurrencyManager cm = (CurrencyManage r)this.dg.Bindi ngContext[this.tCat];

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

// Remember the currently selected row ID
this.currentlyS electedRowId = (int)rv["key"];
}
}
(and here's where the dg.SetDataBindi ng(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.Argumen tNullException' : 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.SetD ataBinding(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
2694
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 important. I use them every day of my life, but I also don't use them every day of my life and there are reasons for both. 2) How much of your process is dependent on reads vs.
45
2453
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 relations to it and so on for every table in the application, this would be a real mess and this has nothing to do with OOP. Normally, would would create a class Customer, a class Invoices, Positions, Articles and so on. But how can this be...
2
2320
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 windows where you can edit information and data, nothing out of the ordinary. I estimate we have something like 50 to 100 tables and/or views in our database each of which map to one strongly typed DataTable. Now we have some odd 20 to 30 typed...
4
1640
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 accesses data in an SQL Server database. I have developed a couple of pages that display data successfully. However, there is one area that I am having trouble getting a handle on, despite purchasing a couple of Wrox books. Up until now, I have...
3
1589
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 fit into the picture. I'm going to be reading some records from a table in a sql db.
0
1222
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 two weeks I discovered that such objects are no more directly usable in pages, namely in DataGrid which don't see them any more. Even if rebuilt in Component Designer, and so visible in code, DataSets are invisible in Page Designer, so now it's...
0
887
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 Nonqueries to enter and retrieve data in and out of DB. Please help. Thanks, Teo
12
3605
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 gridview, for example, I loop through a datareader, populating an array list with instances of a custom class in the middle tier, and then send the array list up to the presentation layer and bind the gridview to it. If I use a typed dataset, I...
1
1786
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 nothing. I can do this in the client for a client specific dataset, but not in the web service. -- Best regards Mark Baldwin
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.