简体   繁体   中英

DataGridView doesn't show data after clone rows

Windows form application. C# 4.0.

Basically I have two datagridviews dgv1 and dgv2. One displays the table correctly. I want to select and clone some rows from dgv1 to dgv2 by clicking on the cells in dgv1.

But the second one doesn't show data at all.

   DataTable dt = new DataTable();
   dgv2.AutoGenerateColumns = true;
   private void btnAdd_Click(object sender, EventArgs e)
   {
        DataRowView currentDataRowView = (DataRowView)dgv1.CurrentRow.DataBoundItem;
        DataRow row = currentDataRowView.Row;
        // add row
        dt.ImportRow(row);
        dgv2.DataSource = dt;
    }

In debug mode, what I found is that

        ?dt.Rows.Count
        1
        ?dt.Columns.Count
        0
        ?dgv2.AutoGenerateColumns
        true

Thanks for advice.

您必须创建列或从另一个表复制模式!

instantiate dt by cloning the datasource of dgv1, ie:

datatable dt = otherdatatable.clone()

where otherdatatable is the datatable datasource of dgv1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM