Can the WinForms DataGrid be used without ADO.Net?

D

Dave Veeneman

I am just getting into the WinForms data grid, and just about everything I
am seeing on it seems to say that the grid must be bound to an ADO.Net
dataset. I want to use the grid as a simple unbound grid-- all I want to do
is write to the grid, read from it, and allow the user to enter text in
particular cells.

Is the WinForms datagrid a viable candidate for this sort of work? If it is,
can you recommend a resource to help me get up to speed using it as a
simple, unbound grid? If it isn't a viable candidate, can you recommend a
third party grid that would be appropriate for this use?

Thanks.
 
M

Michael Mayer

Dave Veeneman said:
I want to use the grid as a simple unbound grid-- all I want to do
is write to the grid, read from it, and allow the user to enter text in
particular cells.

AFAIK, the DataGrid has to be bound to have any functionality.
However, you can create an empty DataTable that takes only
strings and bind to that. I put some code below that sets that up.


--
Michael Mayer
(e-mail address removed)
My CSharp page: http://www.mag37.com/csharp/


DataTable NewTable (int cols, int rows)
{
DataTable dt = new DataTable();
for (int c = 0; c < cols; c++)
{
dt.Columns.Add(new DataColumn("", typeof(string)));
}
for (int r = 0; r < rows; r++)
{
DataRow dr = dt.NewRow();
for (int c = 0; c < cols; c++)
{
dr[c] = String.Empty; // initialize with empty string.
}
dt.Rows.Add(dr);
}
return dt;
}

private System.Windows.Forms.DataGrid dataGrid1;
private DataTable dataTable;
/******** FORM CONSTRUCTOR **/
public Form1() {
InitializeComponent();
this.dataTable = NewTable(4,8);
this.dataGrid1.DataSource = this.dataTable;
// prevent adding of new rows through datagrid
CurrencyManager cm = (CurrencyManager)
this.BindingContext[dataGrid1.DataSource,
this.dataGrid1.DataMember];
((DataView)cm.List).AllowNew = false;

// now setup some values
this.dataTable.Rows[0][0] = "Hi";
this.dataTable.Rows[1][2] = "another string";
}
 
R

Rob Brown

According to MS:
The following data sources are valid:

a.. A DataTable
b.. A DataView
c.. A DataSet
d.. A DataViewManager
e.. Any component that implements the IListSource interface
f.. Any component that implements the IList interface
http://msdn.microsoft.com/library/d...mWindowsFormsDataGridClassDataSourceTopic.asp

A good example can be found here:
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=81

I personally have bound a datagrid to an arraylist and to a collection
class.

Good luck!
 

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