在datagridview中删除新行 [英] Deleting a new row in a datagridview

查看:88
本文介绍了在datagridview中删除新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在datagridiview中有datagridiview,我在单击ADD按钮的行上添加了新行.我还有一个messageBox,如果单击取消"按钮,则应该检查新添加的行是否包含数据,如果它不包含任何数据,则应将其从DGV中删除,如何完成此操作. >
在此先感谢

I am having datagridiview in datagridiview, I''m adding a new row to that on a ADD button click. I''m also having a messageBox, in which If I click Cancel button, I should check whether newly added row contains data or not if it does not contain any data it should be removed from DGV, how to accomplish this.

Thanks in advance

推荐答案

尝试一下,


try this,


if (MessageBox.Show("Some Text", "Alert", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
           {
               DataGridViewCellCollection coll = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells;
               bool flag = false;
               int rowindex = 0;
               foreach (DataGridViewCell cell in coll)
               {
                   if (cell.Value.ToString() != null)
                   {
                       flag = true;
                       rowindex = cell.RowIndex;
                       break;
                   }
               }
               if (!flag)
               {
                   dataGridView1.Rows.Remove(dataGridView1.Rows[rowindex]);
               }
           }



希望对您有帮助



Hope this helps


这篇关于在datagridview中删除新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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