Windows Develop Bookmark and Share   
 index > Windows Forms General > Vertical Datagrid
 

Vertical Datagrid

Hello everybody,
I have been working with datagrids. I have a situation in which I have to bind around 40 column from a table to the grid. NowI dont want the width of the grid to increasebeyongpagelimit...Onewayistouseseveralgrids...
butisthereanycontrolorwayinwhichIcanshowdatajustlikeagridbutvertically i.e. i.e. columns on left side and the rows displayed vertically...
justlikeindetailsviewcontrol...butthedetailsviewcontrolonlyshowsonerow...butihavetoshowalltherowsfromthetable...
Anysuggestions?

Regards
HarisAsghar
Haris.asghar  Wednesday, September 26, 2007 9:07 AM

Hi, Haris,

Based on my understanding, you want to show the Data in your DataGrid vertically, don't you?

I thinkyou can make a Pivot table to achieve your goal.

For example

Code Block

private void Form1_Load(object sender, EventArgs e)

{

DataTable datatable = new DataTable();

datatable.Columns.Add("col1");

datatable.Columns.Add("col2");

datatable.Columns.Add("col3");

datatable.Columns.Add("col4");

datatable.Columns.Add("col5");

datatable.Columns.Add("col6");

datatable.Columns.Add("col7");

datatable.Columns.Add("col8");

datatable.Columns.Add("col9");

datatable.Columns.Add("col10");

datatable.Columns.Add("col11");

datatable.Columns.Add("col12");

datatable.Columns.Add("col13");

datatable.Columns.Add("col14");

datatable.Rows.Add("ABC", "bcd","ABC", "bcd","ABC", "bcd","ABC", "bcd","ABC", "bcd","ABC", "bcd","ABC", "bcd");

datatable.Rows.Add("bcd", "ABC", "bcd", "ABC", "bcd", "ABC", "bcd", "ABC", "bcd", "ABC", "bcd", "ABC", "bcd", "ABC");

DataGrid datagrid = new DataGrid();

datagrid.Width = 300;

datagrid.Height = 400;

datagrid.DataSource = Pivot(datatable);

this.Controls.Add(datagrid);

}

public DataTable Pivot(DataTable source)

{

DataTable dest = new DataTable();

dest.Columns.Add(source.Columns[0].ColumnName);

foreach (DataRow r in source.Rows)

{

dest.Columns.Add(r[0].ToString());

}

for (int i = 1; i < source.Columns.Count; i++)

{

DataRow row = dest.NewRow();

row[0] = source.Columns[i].ColumnName;

dest.Rows.Add(row);

}

for (int i = 0; i < source.Rows.Count; i++)

{

for (int col = 1; col < source.Columns.Count; col++)

{

dest.Rows[col - 1][i + 1] = source.Rows[i][col];

}

}

dest.AcceptChanges();

return dest;

}

Hi, Haris,

Based on my understanding, you want to show the Data in your DataGrid vertically, don't you?

I thinkyou can make a Pivot table to achieve your goal.

For example

Code Block

private void Form1_Load(object sender, EventArgs e)

{

DataTable datatable = new DataTable();

datatable.Columns.Add("col1");

datatable.Columns.Add("col2");

datatable.Columns.Add("col3");

datatable.Columns.Add("col4");

datatable.Columns.Add("col5");

datatable.Columns.Add("col6");

datatable.Columns.Add("col7");

datatable.Columns.Add("col8");

datatable.Columns.Add("col9");

datatable.Columns.Add("col10");

datatable.Columns.Add("col11");

datatable.Columns.Add("col12");

datatable.Columns.Add("col13");

datatable.Columns.Add("col14");

datatable.Rows.Add("ABC", "bcd","ABC", "bcd","ABC", "bcd","ABC", "bcd","ABC", "bcd","ABC", "bcd","ABC", "bcd");

datatable.Rows.Add("bcd", "ABC", "bcd", "ABC", "bcd", "ABC", "bcd", "ABC", "bcd", "ABC", "bcd", "ABC", "bcd", "ABC");

DataGrid datagrid = new DataGrid();

datagrid.Width = 300;

datagrid.Height = 400;

datagrid.DataSource = Pivot(datatable);

this.Controls.Add(datagrid);

}

public DataTable Pivot(DataTable source)

{

DataTable dest = new DataTable();

dest.Columns.Add(source.Columns[0].ColumnName);

foreach (DataRow r in source.Rows)

{

dest.Columns.Add(r[0].ToString());

}

for (int i = 1; i < source.Columns.Count; i++)

{

DataRow row = dest.NewRow();

row[0] = source.Columns[i].ColumnName;

dest.Rows.Add(row);

}

for (int i = 0; i < source.Rows.Count; i++)

{

for (int col = 1; col < source.Columns.Count; col++)

{

dest.Rows[col - 1][i + 1] = source.Rows[i][col];

}

}

dest.AcceptChanges();

return dest;

}

It's my issue. I'm trying your code, Yu Gou.


Batman
BatmanBegins  Tuesday, October 06, 2009 9:15 AM

You can use google to search for other answers

Custom Search

More Threads

• Tabcontrol and pages
• Way to get rid of surplus strings?
• Error when adding a ToolStripMenuItem
• Error MSB3021
• identify "Ctrl+Space" in RichTextBox
• Filter dataGridView based on combo box selection
• Minimized Windows Completely Disappear, however, they can be found in Task Manager as Active/Running programms!
• How long it takes to develop an accounting Application?
• DataGridView cell edit problem
• How do I set control properties between winforms