删除DGV中的选定行C#(Delete Selected Row in DGV C#)

编程入门 行业动态 更新时间:2024-10-20 03:40:29
删除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.

更多推荐

本文发布于:2023-08-07 00:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459018.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Delete   DGV   Row   Selected

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!