1
我创建了一个Windows窗体应用程序。我希望此应用程序能够使用Linq to SQL来搜索记录,然后从数据网格视图中选择该记录并将其删除。从数据网格视图删除
该表单包含一个输入参数,搜索按钮和删除按钮以及数据网格的文本框。
我有搜索部分正常工作,数据网格已填充,但不知道如何实现单击数据网格中的记录并删除它。
更新 - 我已经解决了解决方案。只对btn_Delete_Click事件处理程序进行了更改,因此我在主代码之后包含了他的按钮的更新代码。
namespace DeleteForm
{
public partial class Form1 : Form
{
LinqtoStudentDataContext linqStud = new LinqtoStudentDataContext();
public Form1()
{
InitializeComponent();
}
private void btnDelete_Click(object sender, EventArgs e)
{
}
private void btnSearch_Click(object sender, EventArgs e)
{
var lastName = from stud in linqStud.Students
where txtFind.Text == stud.LastName
select stud;
dataGridView1.DataSource = lastName;
}
}
}
更新代码 -
private void btnDelete_Click(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
//linqStud.Students.DeleteAllOnSubmit();
linqStud.SubmitChanges();
}
}
获取选定的行来自网格,并通过ID删除学生。 –
lazyberezovsky - 我将如何做选定的行部分虽然? – Makin672