Reputation: 607
I have datatable
that has 1 row
and of course caption name columns
,
now I want to show them vertically in a datagridview
for example the datagridview
must have 2 columns
that rows
of one of them shows caption name and the other shows related values of datatable
I try to do that by this code but it shows an exception:
{"There is no row at position 1."}
the code is:
public static void GetSelectedFeedsDetails(Form1 frm)
{
string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from SelectedFeeds where ID=" + frm.SelectedFeedlistBox.SelectedValue, Connection);
DataTable OldDTable = new DataTable();
DataA.Fill(OldDTable);
DataTable NewDTable = new DataTable();
NewDTable.Columns.Add("Feild Name");
for (int i = 0; i < OldDTable.Rows.Count; i++)
{
NewDTable.Columns.Add();
}
for (int i = 0; i < OldDTable.Columns.Count; i++)
{
DataRow NewRow = NewDTable.NewRow();
NewRow[0] = OldDTable.Columns[i].Caption;
for(int j = 0; j < OldDTable.Columns.Count; j++)
{
NewRow[j + 1] = OldDTable.Rows[j][i];
NewDTable.Rows.Add(NewRow);
}
}
frm.dataGridView1.DataSource = NewDTable;
}
what should I do?
Upvotes: 1
Views: 1390
Reputation: 607
as OhBeWise said the answer is:
DataTable NewDTable = new DataTable();
NewDTable.Columns.Add("Field Name");
for (int i = 0; i < OldDTable.Rows.Count; i++)
{
NewDTable.Columns.Add();
}
for (int i = 0; i < OldDTable.Columns.Count; i++)
{
DataRow NewRow = NewDTable.NewRow();
NewRow[0] = OldDTable.Columns[i].Caption;
for (int j = 0; j < OldDTable.Rows.Count; j++)
{
NewRow[j + 1] = OldDTable.Rows[j][i];
}
NewDTable.Rows.Add(NewRow);
}
Upvotes: 1