HNow从GridView删除一行 [英] HNow to delete a row from GridView

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

问题描述

请帮我解决Gridview中删除行的编码

Pleases help me out on coding of the deleting row from Gridview

推荐答案

GridView.DeleteRow方法 [ ^ ]
在ASP.NET中的GridView中删除多行 [ ^ ]

以及更多类似的线程
How to delete row from gridview?[^]

Look at: GridView.DeleteRow Method[^]
Deleting Multiple Rows in GridView in ASP.NET[^]

and more similar threads here[^]


假设您有一个gridview" gdvContacts和cellclick事件如下方法.
还要假设您在网格视图上有一个删除"按钮.

Assuming, you have a gridview "gdvContacts" and cellclick event as below method.
Also assuming you have a Delete button on grid view.

private void gdvContacts_CellClick(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        if (!(e.RowIndex < 0 || e.ColumnIndex < 0))
        {
            DataGridViewButtonCell cell = gdvContacts.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewButtonCell;
            if (cell != null)
            {
                string strTest = cell.OwningColumn.HeaderText;
                if (strTest == "Delete" || strTest == "Update")
                {
                    if (strTest == "Delete")
                    {
                        DialogResult result;
                        result = MessageBox.Show("Are you sure want to delete the selected contact?", "Delete Contact ?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        if (result == DialogResult.OK)
                        {
                            string ID = gdvContacts.Rows[e.RowIndex].Cells[e.ColumnIndex - 4].Value.ToString();
                             this.gdvContacts.Rows.RemoveAt(cell.RowIndex);
                        }
                    }



                }
            }
            gdvContacts.EditMode = DataGridViewEditMode.EditOnEnter;
        }

    }
    catch { }
}


如果没有删除按钮,请在填充gridview之后添加它:


if you do not have Delete button, add it after populate gridview :

if (this.gdvContacts.Rows.Count > 0)
            {

                DataGridViewButtonColumn dt = new DataGridViewButtonColumn();
                dt.HeaderText = "Delete";
                dt.Text = "Delete";
                dt.Name = "Delete";
                dt.ToolTipText = "Delete this row";
                dt.Width = 55;
                dt.UseColumnTextForButtonValue = true;
                gdvContacts.Columns.Add(dt);
}


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

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