blah blah blah is here! blah blah » Close

up0down
link

Dear All,

Does C# has a table? I want to display my value in several rows and columns. Any idea how?

Thank you in advance :)

Kindest Regards,




E

last answered one year ago

4 answers

link

to use the grid view, drag and drop it on the form, then make a datatable for it and set the datasource of the gridview to that datatable.

private System.Data.DataTable table;
void CreateTable(string[] columns)
{
table = new System.Data.DataTable();
foreach (string col in columns)
{
table.Columns.Add(new System.Data.DataColumn(col));
}
}

void AddRows(List<string[]> rows)
{
foreach (string[] row in rows)
{
System.Data.DataRow r = table.NewRow();
for(int i=0;i<row.Length;i++)
r[i] = row[i];
table.Rows.Add(r);
}
}

void MakeGrid()
{
string[] cols ={ "First", "Second", "Third" };
CreateTable(cols);//fills the data table with columns called First, Second and Third
List<string[]> rows = new List<string[]>();
string[] firstRow ={"1","2","3"};
string[] secondRow ={"4","5","6"};
string[] thirdRow ={ "7", "8", "9" };
string[] fourthRow ={ "10", "11", "12" };
rows.Add(firstRow);
rows.Add(secondRow);
rows.Add(thirdRow);
rows.Add(fourthRow);
AddRows(rows);//fills the data table with the rows according to the predefined columns
this.dataGridView1.DataSource = table;//merge the datatable with the datagridview
}


calling MakeGrid() will create your datatable with some sample data and bind it to the dataGridView in the form.

up0down
link

I found out that we can use Data grid view for making the table, any idea on how to use this? I am still very beginner in c sharp :(

up1down
link

If you're also interested in binding your DataGridView to a SQL Server database or to a collection of objects, then the Dot Net Perls website has some useful articles.

Check out the links on this page.

up1down
link

Like the answers above, I also used DataGridView, and I recomend it.
It is a powerfull tool that not only display the data as a table, it's a dinamic table and can be used in some nice ways...
good luck with it.

Feedback