0

アプリケーションに 2 つのデータグリッドがあります (dataGridView1dataGridView2)。選択したアイテムを からdataGridView1に移動してdataGridView2います。これが私が現在これを行っている方法です:

DataTable dt = new DataTable("dt");
DataTable id = new DataTable("id");
try
{
    if (dataGridView1.Rows.Count > 0)
    {
        for (int i = 0; i < dataGridView1.Rows.Count - 0; i++)
        {
            if (dataGridView1.Rows[i].Cells[0].Value != null)
            {
                DataRow row;
                DataRow idRow;
                row = dt.NewRow();
                idRow = id.NewRow();
                idRow["id"] = dataGridView1.Rows[i].Cells[1].Value.ToString();
                row["id"] = dataGridView1.Rows[i].Cells[1].Value.ToString();
                row["Link Name"] = dataGridView1.Rows[i].Cells[2].Value.ToString();
                dt.Rows.Add(row);
                id.Rows.Add(idRow);
            }
        }
    dataGridView2.DataSource = dt;
}

ただし、からアイテムを削除できる必要もありますdataGridView2。現在、DataTabledtはバインドされてdataGridView2おり、アイテムが追加された後にクリアすることはできませんdataGridView2

基本的に、私の質問は、を使用せずにデータ テーブルの内容をデータ グリッドに追加する方法はありますdatagrid.DataSourceか?

4

1 に答える 1

1

関数を使用して各行を手動で追加できdataGridView2.Rows.Addます。また、通常dataGridView2.Row(n).Tag、その行のデータの実際のソースを保持するために を使用しました。

int n = dataGridView2.Rows.Add();
DataGridViewRow newRow = dataGridView2.Rows[n]; 
newRow.Cells[0].Value  = "ABC";
newRow.Cells[1].Value  = 123;
newRow.Tag = row;
于 2012-07-30T12:40:19.090 に答える