2
Answers

Datagrid View Header Show as Row Wise

Hello Friends,
 
I am new in C# and want to know  "way to change data grid view Header show as ROW wise".
 
I have datagrid view in my form with One Row Only after filling it with Data-Table it showing like:
 
 Column 1 Column 2 Column 3 Column 4
 Value-1 Value-2 Value-3 Value-4
 
** Values are of Data-Table
  
Now I need it in this way :
 
 Column 1 Value-1
 Column 2 Value-2
 Column 3  Value-3
 Column 4  Value-4
 
 Please help.
 
Regards
Ravi 
 

Answers (2)

0
Photo of Prem Anandh J
NA 492 32k 9y
Try this,
var tbl = dset.Tables["Stock"];
var swappedTable = new DataTable();
for (int i = 0; i <= tbl.Rows.Count; i++)
{
 swappedTable.Columns.Add(Convert.ToString(i));
}
for (int col = 0; col < tbl.Columns.Count; col++)
{ var r = swappedTable.NewRow();
r
[0] = tbl.Columns[col].ToString();
 for (int j = 1; j <= tbl.Rows.Count; j++)
  r
[j] = tbl.Rows[j - 1][col];
  swappedTable
.Rows.Add(r);
}
dataGridView1
.DataSource = swappedTable;

-------------------------------------------------------------------
If you find anything useful please accept my answer


0
Photo of Sivaraman Dhamodaran
NA 36.3k 3.4m 9y
Access the column collection and  set the property (How many column you want to freeze):
 
 
//prevents column from scrolling out of view
.Columns(i).Frozen = True
//make it look like a header
.Columns(i).DefaultCellStyle = .RowHeadersDefaultCellStyle
//Edit not allowed
.Columns(i).ReadOnly = True
 
Here i is the column index you want to freeze. You can even set more than one column as header also.
Also seach for removing the Header Row.