从c#中的datagridview和数据库中删除选定的行 [英] delete selected row from datagridview and database in c#

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

问题描述

从cg

推荐答案

中删除datagridview和数据库中的选定行使用此代码从DataGridView中删除行



Use this code to Delete Row From DataGridView

if (this.dataGridView1.SelectedRows.Count > 0)
            {
                dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
            }





和此代码从数据库中删除数据



首先你在Temp变量中找到并存储主要字段数据,并在where子句中传递这个变量



Ex: -



如果您的主要字段名称是Emp_Id并且它在Grid中的位置是单元格[0]

那么



and This code for Delete data from Database

first of all you find and store primary field data in a Temp variable and pass this variable in where clause

Ex:-

if your primary field name is Emp_Id and it's it's position in Grid is cell[0]
then

int Primary_Field_Value =dataGridView1[Column_Index,Row_Index].value.Tostring();

SqlCommand cmd = new SqlCommand ("Delete from table where id='"+ Primary_Field_Value +"'",ConnectionObject); 
cmd.ExecutenonQuery(); 





谢谢&注意

Sham:)



Thanks & Regard
Sham :)


deletedatagridrows.aspx [ ^ ]



如何从数据网格数据库和数据库中删除所选行[ ^ ]



从Datagridview中删除行并在数据库中删除相同的更新 [ ^ ]



检查链接..希望它会有所帮助。 。
deletedatagridrows.aspx[^]

how-to-delete-a-selected-row-from-datagridview-and-database[^]

Delete row from Datagridview and same updates in database[^]

Check the links..hope it will help..


在源代码中执行此操作:



In source part do this:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField HeaderText="User Name">
                <ItemTemplate>
                    <%#Eval("userName")%>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Password">
                <ItemTemplate>
                    <%#Eval("pass")%>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Test">
                <ItemTemplate>
                    <%#Eval("test")%>
                </ItemTemplate>
            </asp:TemplateField>

             <asp:TemplateField >
                <ItemTemplate>
                    <a href="Default.aspx?id=<%#Eval("userId") %>&act=del">Delete</a>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>



//然后在代码后面:


// Then in code behind:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string qact = Request.QueryString["act"];
            if (qact == "del")
            {
                string id = Request.QueryString["id"];
                string str = ConfigurationManager.ConnectionStrings["class28"].ToString();
                SqlConnection con = new SqlConnection(str);
                string sql = "delete from t_User where userId=" + int.Parse(id) + "";
                SqlCommand cmd = new SqlCommand(sql, con);
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }


这篇关于从c#中的datagridview和数据库中删除选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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