1

我在 DataGridView 中有数据,我想通过选择从中删除一行。我在保存删除查询的表单上有一个按钮 DELETE

var note = (from n in dataCont.Past_Notes where n.ID == ? select n).First();

如果我在代码中手动指定一个 ID,它会完美运行。(问号在哪里)

但我不想这样。如何获取所选行的索引,然后删除该行?

 private void button2_Click(object sender, EventArgs e)
 {
     Past_Sticky_NotesDataContext dataCont = new Past_Sticky_NotesDataContext();

     var note = (from n in dataCont.Past_Notes
                 where n.ID == ?
                 select n).First();

     dataCont.Past_Notes.DeleteOnSubmit(note);
     dataCont.SubmitChanges();
 }

必须使用 LINQ 查询。

4

1 回答 1

0

You need to Set DataGridView.MultiSelect=false and DataGridView.SelectionMode = FullRowSelect. then your_dataGridView_name.SelectedRows[0] - this will return selected row. Then you get value of id cell in your row selectedRow.Cells[your_id_index]. And use it in LINQ query
Edit
Or you can use another property to get your selectedRow index datagridview.CurrentCell.RowIndex

于 2012-12-16T21:48:43.653 回答