在 DataGridView (Winforms) 上将数据源行显示为列
Show datasource rows as columns on a DataGridView (Winforms)
我想在 DataGridView 上显示我所有的数据源行,但不是作为行而是作为一行的列。每检索到 12 个项目,我想在 DataGridView 上插入一个新行,并用 DataSource 中的更多项目填充这个新行(每行 12 个)。
我的 DataSource 每个只检索一个项目,直接将它与 DataGridView 一起使用效果很好,但每个项目显示不同的行。
有什么提示吗?
感谢@SriramSakthivel。
private void AddToList(DataTable dt)
{
possibleWords = 0;
// Cleans the data grid view
WordList.DataSource = null;
WordList.Refresh();
// Let's transform the original data table onto another, changing rows by columns
DataTable table = new DataTable();
for (int i = 0; i < 10; i++)
{
table.Columns.Add(Convert.ToString(i));
}
DataRow r;
int col = 0;
//for (int k = 0; k < dt.Columns.Count; k++)
{
r = table.NewRow();
for (int j = 0; j < dt.Rows.Count; j++)
{
if (col >= 10)
{
table.Rows.Add(r);
col = 0;
r = table.NewRow();
}
r[col++] = (dt.Rows[j][0]).ToString().ToUpper();
possibleWords++;
}
table.Rows.Add(r);
}
// Puts the new data table as datasource of the word list
DataView dv = table.DefaultView;
WordList.DataSource = dv;
if (possibleWords == 0)
return;
WordList.Columns[0].DefaultCellStyle.BackColor = Color.WhiteSmoke;
WordList.ColumnHeadersVisible = false;
WordList.RowHeadersVisible = false;
}
我想在 DataGridView 上显示我所有的数据源行,但不是作为行而是作为一行的列。每检索到 12 个项目,我想在 DataGridView 上插入一个新行,并用 DataSource 中的更多项目填充这个新行(每行 12 个)。
我的 DataSource 每个只检索一个项目,直接将它与 DataGridView 一起使用效果很好,但每个项目显示不同的行。
有什么提示吗?
感谢@SriramSakthivel。
private void AddToList(DataTable dt)
{
possibleWords = 0;
// Cleans the data grid view
WordList.DataSource = null;
WordList.Refresh();
// Let's transform the original data table onto another, changing rows by columns
DataTable table = new DataTable();
for (int i = 0; i < 10; i++)
{
table.Columns.Add(Convert.ToString(i));
}
DataRow r;
int col = 0;
//for (int k = 0; k < dt.Columns.Count; k++)
{
r = table.NewRow();
for (int j = 0; j < dt.Rows.Count; j++)
{
if (col >= 10)
{
table.Rows.Add(r);
col = 0;
r = table.NewRow();
}
r[col++] = (dt.Rows[j][0]).ToString().ToUpper();
possibleWords++;
}
table.Rows.Add(r);
}
// Puts the new data table as datasource of the word list
DataView dv = table.DefaultView;
WordList.DataSource = dv;
if (possibleWords == 0)
return;
WordList.Columns[0].DefaultCellStyle.BackColor = Color.WhiteSmoke;
WordList.ColumnHeadersVisible = false;
WordList.RowHeadersVisible = false;
}