Hello All,
I now have a data grid view which displays the records in rows . Is there a way to store the data in columns . In short , i want to interchange the rows and columns so that i have row headers in place of column headers??
Thanks for the read!
AVink
Loading
Nilanka DharmadasaPosted Jan 7, 2010, 12:32 AM
I hope you can obtain your orginal datatable from your XML file.
Then you can pivot that datatable and bind it to the datagridview.
I found a good exmple on the web. I modified it a bit and uploaded.
Check it. And use it for your application.
If you find this answer useful, please tick 'Do you like this answer' checkbox.
anuradha shivPosted Jan 6, 2010, 11:42 PM
In my app, i use an xml file to populate the data grid view. How do i apply the Pivot and unpivot concept to my need?
I have here a piece of my method that populates the data grid view .. May be it helps u understand my query better . HEre in my datagrid view i have column headers for BaudRate and Physical MEdia . They must now appear as row headers in place of column headers
public void PopulateGrid(DataSet dataSets, String tableName)
{
InitializeComponent();
int countOfColumns = 0;
DataGridViewColumn[] gridColumns = null;
this.dataSet = dataSets;
bindingSource.DataSource = dataSets.Tables[tableName];
this.DataMember = "CommunicationDescription";
countOfColumns = dataSet.Tables[tableName].Columns.Count - 1;
gridColumns = new System.Windows.Forms.DataGridViewColumn[countOfColumns];
for (int i = 0; i < countOfColumns; i++)
{
String appendText = dataSet.Tables[tableName].Columns[i].Caption;
switch (appendText)
{
case "PhysicalMedia":
{
DataGridViewComboBoxColumn column;
column = (DataGridViewComboBoxColumn)new System.Windows.Forms.DataGridViewComboBoxColumn();
column.Items.Add("Ethernet");
column.Items.Add("RS232");
column.DataPropertyName = dataSet.Tables[tableName].Columns[i].Caption;
column.HeaderText = dataSet.Tables[tableName].Columns[i].Caption;
column.Name = appendText + "DataGridViewColumn";
gridColumns[i] = column;
}
break;
case "BaudRate":
{
DataGridViewComboBoxColumn column;
column = new System.Windows.Forms.DataGridViewComboBoxColumn();
column.Items.Add("4800");
column.Items.Add("9600");
column.Items.Add("19200");
column.Items.Add("57600");
column.DataPropertyName = dataSet.Tables[tableName].Columns[i].Caption;
column.HeaderText = dataSet.Tables[tableName].Columns[i].Caption;
column.Name = appendText + "DataGridViewColumn";
gridColumns[i] = column;
}
break;
}
Nilanka DharmadasaPosted Jan 6, 2010, 11:33 PM
You can do that using pivot table.
Let's say for your datagridview, you bind data from following query.
'Select * from Employee'.
Now if you want to interchange your rows and columns of your datagridview, you should write a query to get the pivot table of your previous query.
Then you can directly bind it to your datagridview. It will show interchanged rows and columns.
To get an idea of how to get a pivot table, you can go to following link. It has exmples.
Click here.
If you have any problem, please ask.
If you think I directed you to the correct path, please tick 'Do you like this answer' checkbox.
anuradha shivPosted Jan 6, 2010, 11:26 PM
i did not actually get u .. could u be more specific. What i was intendin to do was interchange columns and rows... in a DataGridView. i did not get this exactly ..What with the Caption?