如何设置datagridview列是datagridview

时间:2016-05-05 06:28:55

标签: c# winforms datagridview

我必须在我的项目中使用datagridview,因此需要在datagridview的左侧显示datagridview列,例如,

             column 1       column 2
-------------------------------------------
column 1 

column 2 

...

`// TODO: This line of code loads data into the 'hRPayDataSet.employee' table. You can move, or remove it, as needed.

 this.employeeTableAdapter.Fill(this.hRPayDataSet.employee);       
 dataGridView1.DataSource =employeeBindingSource;

 DataTable table2 = (DataTable)(dataGridView1.DataSource);
 dataGridView1.DataSource = null;
 DataTable tTable = null;
 try { tTable = GenerateTransposedTable(table2); }
 catch (Exception ex) { MessageBox.Show(ex.Message); }
 dataGridView1.DataSource = tTable;
 dataGridView1.RowHeadersVisible = true;
 dataGridView1.ColumnHeadersVisible = false;`

请帮帮我....

1 个答案:

答案 0 :(得分:0)

您可以尝试这样做:首先将DataGridView绑定到DataTable

DataTable table = (DataTable)(dataGridView1.DataSource);
DataTable tTable = null;
try
{
    tTable = GenerateTransposedTable(table);
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message);
}

dataGridView1.DataSource = tTable;
dataGridView1.RowHeadersVisible = true;
dataGridView1.ColumnHeadersVisible = false;

GenerateTransposedTable转置您的table

private DataTable GenerateTransposedTable(DataTable inputTable)
{
    DataTable outputTable = new DataTable();

    outputTable.Columns.Add(inputTable.Columns[0].ColumnName.ToString());

    foreach (DataRow inRow in inputTable.Rows)
    {
        string newColName = inRow[0].ToString();
        outputTable.Columns.Add(newColName);
    }

    for (int rCount = 0; rCount <= inputTable.Columns.Count - 1; rCount++)
    {
        DataRow newRow = outputTable.NewRow();

        newRow[0] = inputTable.Columns[rCount].ColumnName.ToString();
        for (int cCount = 0; cCount <= inputTable.Rows.Count - 1; cCount++)
        {
            string colValue = inputTable.Rows[cCount][rCount].ToString();
            newRow[cCount + 1] = colValue;
        }
        outputTable.Rows.Add(newRow);
    }

    return outputTable;
}

测试代码:

private void Form1_Load(object sender, EventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("A");
    table.Columns.Add("B");
    table.Columns.Add("C");
    table.Columns.Add("D");
    table.Columns.Add("E");
    table.Columns.Add("F");

    dataGridView2.DataSource = table;

    DataTable table2 = (DataTable)(dataGridView2.DataSource);
    DataTable tTable = null;
    try
    {
        tTable = GenerateTransposedTable(table2);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

    dataGridView2.DataSource = tTable;
    dataGridView2.RowHeadersVisible = true;
    dataGridView2.ColumnHeadersVisible = false;
}

更新2:

用以下代码替换您的代码:

// TODO: This line of code loads data into the 'hRPayDataSet.employee' table. You can move, or remove it, as needed.

this.employeeTableAdapter.Fill(this.hRPayDataSet.employee);
dataGridView1.DataSource = employeeBindingSource;

var bds = (BindingSource)(dataGridView1.DataSource);
HRPayDataSet ds = (HRPayDataSet)bds.DataSource;

DataTable dt = TransposeTable(ds.Tables[0]);
dataGridView1.Columns.Clear();
dataGridView1.DataSource = null;
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dt;

TransposeTable()方法:

private DataTable TransposeTable(DataTable inputTable)
{
    DataTable outputTable = new DataTable();

    for (int i = 0; i < inputTable.Rows.Count; i++)
    {
        outputTable.Columns.Add("col" + i);
    }

    for (int rCount = 0; rCount <= inputTable.Columns.Count - 1; rCount++)
    {
        DataRow newRow = outputTable.NewRow();
        newRow[0] = inputTable.Columns[rCount].ColumnName.ToString();
        for (int cCount = 0; cCount <= inputTable.Rows.Count - 2; cCount++)
        {
            string colValue = inputTable.Rows[cCount][rCount].ToString();
            newRow[cCount + 1] = colValue;
        }
        outputTable.Rows.Add(newRow);
    }
    return outputTable;
}