删除DGV中的选定行C#(Delete Selected Row in DGV C#)
我的问题是:
我想从所选行中检索数据(row.Cells [4] .Value),然后在变量中分配它。 获取数据后,我想使用按钮删除选定的行。这是我的代码:
private void btnDelete_Click(object sender, EventArgs e) { if (dataGridViewPOS.SelectedRows.Count > 0) { string qty = row.Cells[4].Value; dataGridViewPOS.Rows.Remove(dataGridViewPOS.SelectedRows[0]); } }My Problem is:
I want to retrieve the data(row.Cells[4].Value) from the selected row then assign it in a variable. After getting the data I want to delete the selected row using a button.Here is my code:
private void btnDelete_Click(object sender, EventArgs e) { if (dataGridViewPOS.SelectedRows.Count > 0) { string qty = row.Cells[4].Value; dataGridViewPOS.Rows.Remove(dataGridViewPOS.SelectedRows[0]); } }最满意答案
你的代码的问题是你没有定义row变量。
尝试:
if (dataGridViewPOS.SelectedRows.Count > 0) { var row = dataGridViewPOS.SelectedRows[0]; string qty = row.Cells[4].Value; dataGridViewPOS.Rows.Remove(dataGridViewPOS.SelectedRows[0]); }编辑
这只是Alex评论的答案,我在回答后意识到了。
The problem with your code is that you haven't defined the row variable.
Try:
if (dataGridViewPOS.SelectedRows.Count > 0) { var row = dataGridViewPOS.SelectedRows[0]; string qty = row.Cells[4].Value; dataGridViewPOS.Rows.Remove(dataGridViewPOS.SelectedRows[0]); }Edit
This is just an answer-version of Alex's comment, I realized it after answering.
更多推荐
发布评论