如何分配行变量以便能够删除它。 。 。 [英] How to assign row variable to be able to delete it . . .

查看:93
本文介绍了如何分配行变量以便能够删除它。 。 。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DataGridView受DataTable(csvData)的约束。由于网格镜像桌子,我在想。 。 。 this.DataGridView1.SelectedRows.Count将是表的同一行。但是,如何将所选行分配给表的行(DataRow),以便删除它?

 private void DeleteRowButton_Click(object sender,EventArgs e)
{

if(this.DataGridView1.SelectedRows.Count> 0& ;&
this.DataGridView1.SelectedRows [0] .Index!=
this.DataGridView1.Rows.Count - 1)

{

// DataRowCollection rowCollection = csvData.Rows.Count(); ???
//rowCollection.Remove(); ????没有帮助

DataRow行;
row.Delete(); //未分配行?


}
}

解决方案

假设您的数据源可在该事件内访问,假设代码中的索引确实指向DataSource上DataRow的索引,您可以尝试执行以下操作:



  int  index =  this  .DataGridView1.SelectedRows [ 0 ].Index; 
yourDataSource.Rows [index] .Delete();
yourDataSource.AcceptChanges();


试试这个..







 私人  void  button1_Click( object  sender,EventArgs e)
{
// < span class =code-comment> foreach(dataGridView1.SelectedRows中的DataGridViewRow行)//用于行选择
foreach (DataGridViewCell cell < span class =code-keyword> in dataGridView1.SelectedCells)
{
var rowIndex = cell.RowIndex;
dataTable.Rows [rowIndex] .Delete();
dataTable.AcceptChanges();

}
} < / pre >


My DataGridView is bound by DataTable (csvData). Since the grid mirrors the table, I'm thinking . . . this.DataGridView1.SelectedRows.Count would be the same row of the table. But how do I assign that selected row to the row (DataRow) of the table so I can delete it?

private void DeleteRowButton_Click(object sender, EventArgs e)
        {

            if (this.DataGridView1.SelectedRows.Count > 0 &&
           this.DataGridView1.SelectedRows[0].Index !=
           this.DataGridView1.Rows.Count - 1)

            {

              //  DataRowCollection rowCollection = csvData.Rows.Count();  ???
              //rowCollection.Remove();  ???? no help

                DataRow row;
                row.Delete();  // row not assigned ?

                
             }
          }

解决方案

Assuming your DataSource is accessible inside that event, and assuming the index in your code really points to the index of the DataRow on your DataSource, you can try doing the following:

int index = this.DataGridView1.SelectedRows[0].Index;
yourDataSource.Rows[index].Delete();
yourDataSource.AcceptChanges();


Try this..



private void button1_Click(object sender, EventArgs e)
        {
           // foreach (DataGridViewRow row in dataGridView1.SelectedRows) // for row selection
            foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
            {
                var rowIndex = cell.RowIndex;
                dataTable.Rows[rowIndex].Delete();
                dataTable.AcceptChanges();

            }
        }</pre>


这篇关于如何分配行变量以便能够删除它。 。 。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆