C# DataSet

Started by
8 comments, last by GameDev.net 18 years, 4 months ago
For a couple of days now I've been trying my utmost to get a table with cells to display on a form of mine, but I can never get it right. All I can display is a table, but the colums and rows never work. Just about all tutorials I've looked at, including a C# book of mine, include SQL into the tutorial which has completely thrown me off track. Any help would be greatly appreciated. Here is the code: using System; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.Collections; namespace DefaultNamespace { public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } DataGridTextBoxColumn colCar = new DataGridTextBoxColumn(); private DataGrid myDataGrid; [STAThread] public static void Main(string[] args) { Application.Run(new Form1()); } private void InitializeComponent() { myDataGrid = new System.Windows.Forms.DataGrid(); DataColumn Column = new DataColumn(); // // myDataGrid // myDataGrid.CaptionText = "Category One"; myDataGrid.Location = new System.Drawing.Point(400, 24); myDataGrid.Name = "myDataGrid"; myDataGrid.Size = new System.Drawing.Size(200, 456); myDataGrid.TabIndex = 0; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(1024, 751); this.Controls.Add(this.myDataGrid); this.Name = "Form1"; this.Text = "DataGrid Test"; } } }
Advertisement
15 views and no replies? Surely someone knows how to add the columns and rows. :(
I dont really have any windows application programming experience, but since no one is jumping in...

Where are you expecting the data to come from to fill your grid? It has to come from somewhere, usually a table in a database. You need to use SQL to get retreive this data.

Like I said, I have no windows app experience, but if it is similar to web applications (which I would bet it is), you set the .DataSource of the datagrid to = a DataReader, which has data bound to it via an SQL Query (NOTE: this isn't the only way, you can do it several ways depending on how you want the data to sit in memory). Then you call .BindData() on the grid.

It looks something like: (NOTE: this uses Sql* objects, which are specific to only SQL Server (MSSQL). Replace "Sql" with "OleDb" (or something like that) to get the database agnostic versions.

string query = "select * from some_table";SqlConnection conn = new SqlConnection(connection_string);SqlCommand comm = new SqlCommand(query,conn);conn.Open();SqlDataReader reader = comm.ExecuteReader();myDataGrid.DataSource = reader;myDataGrid.BindData();


Really your best bet is to just read tutorials on this... there are plenty on google. If you are lost with the SQL, then read some SQL tutorials first.
FTA, my 2D futuristic action MMORPG
Are you using this in Visual Studio 2005 or 2003? The 2003 version of C# sucks horribly at using things related to databases. From the looks of it your doing it non-programmed, such as just using the form designer. I recently finished a C# class we used 2003 we did all things like data sets through the code, because the other way just did not work.

The only thing I didn't do through the code were things like data lists, and data grids.

You can setup the code by adding things like:
	private DataSet			northWindsDataSet;	private OleDbConnection		northWindsOleDbConnection;	private OleDbCommand		northWindsOleDbCommand;	private OleDbDataAdapter	northWindsOleDataAdapter;	private OleDbCommandBuilder	northWindsOleCommandBuilder;

Then use there functions and methods within the code to set it up, there is no easy way for databases in 2003.
- GDKnight
another example would be to construct the dataset in your code (assuming c# 2.0, note: there are quite a number of ways to do this)

DataSet ds = new DataSet();
ds.Tables.Add("tablename");
ds.Tables[0].Columns.Add("name1");
ds.Tables[0].Columns.Add("name2");

ds.Tables[0].Rows.Add(new object[] {"data1", "data"});

myDataGrid.DataSource = ds.Tables[0].DefaultView;
Thanks for all the replies.

@GDKnight: I'm using VS 2003. It may look like I used the Designer, but originally I hadn't. When I switched into Design view it swapped all my code, is there a way of disabling that?

@graveyard filla: That is exactly what I'm not looking for. I don't want to use SQL, or any database for that matter. All I want to do is create a table which let's the user add data themselves, like the one in the picture below.

http://www.c-sharpcorner.com/Code/2002/June/PrintPreviewProperties.jpg

How could I get the cells to have list boxes within them?

Thanks again.

Here is code again:

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;

namespace DefaultNamespace
{
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}

private DataGrid myDataGrid;
private DataGrid myDataGridTwo;
private System.Data.DataColumn Column;
private System.Data.DataSet ds;
private System.Data.DataSet dsTwo;


[STAThread]
public static void Main(string[] args)
{
Application.Run(new Form1());
}

private void InitializeComponent()
{
this.myDataGridTwo = new System.Windows.Forms.DataGrid();
this.myDataGrid = new System.Windows.Forms.DataGrid();
this.Column = new System.Data.DataColumn();
this.ds = new System.Data.DataSet();
this.dsTwo = new System.Data.DataSet();
this.dataColumn1 = new System.Data.DataColumn();
this.dataColumn2 = new System.Data.DataColumn();
this.dataColumn3 = new System.Data.DataColumn();
this.dataColumn4 = new System.Data.DataColumn();
//
// myDataGridOne
//
this.myDataGrid.CaptionText = "Category One";
this.myDataGrid.DataMember = "";
this.myDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.myDataGrid.Location = new System.Drawing.Point(344, 140);
this.myDataGrid.Name = "myDataGridTwo";
this.myDataGrid.Size = new System.Drawing.Size(189, 456);
this.myDataGrid.TabIndex = 0;
//
// Add tables to myDataGridOne
//
DataSet ds = new DataSet();
ds.Tables.Add("tablename");
ds.Tables[0].Columns.Add("name1");
ds.Tables[0].Columns.Add("name2");

for(int i=0; i < 10; i++)
{
ds.Tables[0].Rows.Add(new object[] {"data" + i, "data" + i});
}

myDataGrid.DataSource = ds.Tables[0].DefaultView;
//
// myDataGridTwo
//
this.myDataGridTwo.CaptionText = "Category Two";
this.myDataGridTwo.DataMember = "";
this.myDataGridTwo.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.myDataGridTwo.Location = new System.Drawing.Point(144, 40);
this.myDataGridTwo.Name = "myDataGridTwo";
this.myDataGridTwo.Size = new System.Drawing.Size(189, 456);
this.myDataGridTwo.TabIndex = 1;
//
// Add Tables to myDataGridTwo
//
DataSet ds2 = new DataSet();
ds2.Tables.Add("tablename2");
ds2.Tables[0].Columns.Add("name3");
ds2.Tables[0].Columns.Add("name4");

for(int i=0; i < 10; i++)
{
ds2.Tables[0].Rows.Add(new object[] {"data" + i, "data" + i});
}

myDataGridTwo.DataSource = ds2.Tables[0].DefaultView;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(1024, 751);
this.Controls.Add(this.myDataGridTwo);
this.Controls.Add(this.myDataGrid);
this.Name = "Form1";
this.Text = "DataGrid Test";
}
}
}

[Edited by - KurtLives on December 19, 2005 3:35:11 AM]
Shameless bump.

See last post.
Quote:Original post by KurtLives
@GDKnight: I'm using VS 2003. It may look like I used the Designer, but originally I hadn't. When I switched into Design view it swapped all my code, is there a way of disabling that?

Well from the looks of your code your putting all of the actual code inside the area that the design view changes. This is why it keeps swapping your code around, and why you probably can see it in design view. Avoid design view completely if your doing a database in 2003, with the exception of data lists and data grids; also take all of your code out of the InitializeComponent method.

private void InitializeComponent()

Is one area the design view will change a lot. I would advise against putting your code here. Instead perhaps if you really need your code there put it in a custom method, or you could put your code in the Load Form method, which is where I would say is a better spot for it.
- GDKnight
GDKnight, thanks for the reply. Do all designers tend to do that? Before I used VS, I used SharpDevelop, and it was worse. It actually removed code, but didn't reply it with anything "better".

By any chance would you know how to draw tables like this?

http://www.c-sharpcorner.com/Code/2002/June/PrintPreviewProperties.jpg

I've been fiddling with ListBoxes but I can't get them to appear like that. Any ideas?
the image you referred to is showing a windowsforms propertygrid control. see this article how to use it properly: http://www.codeproject.com/csharp/PropertyGrid.asp

This topic is closed to new replies.

Advertisement