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

问题描述

大家好,

我想删除gridview中的行.

但是我无法从要从gridview删除行的表中选择id.

我正在使用此处显示的代码从所选行中选择单元格值.
有一些我可以尝试的方式
1)TextBox1.Text = GridView1.Rows [e.RowIndex] .Cells(0).Text;
2)字符串nam =((Label)GridView1.Rows [e.RowIndex] .Cells [0] .Controls [0]).Text;

3)int a =(int)GridView1.DataKeys [e.RowIndex] .Value;

4)GridViewRow行=(GridViewRow)GridView1.Rows [e.RowIndex];
标签lbl =(Label)row.FindControl("lblid");

但是我无法删除任何一个指导我或向我提供任何参考代码.


谢谢&问候
Deepak

Hi All,

I want to delete row in the gridview.

But i am not able to pick the id from the table on which base i am delete the row from gridview

I am using code which is show here for select the cell value form the selected row.
there some way which i am try in this give below
1) TextBox1.Text = GridView1.Rows[e.RowIndex].Cells(0).Text;
2)string nam = ((Label)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;

3)int a = (int)GridView1.DataKeys[e.RowIndex].Value;

4) GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbl = (Label)row.FindControl("lblid");

But i am not able to delete the any one guide me or provide me any reference code.


Thanks & Regards
Deepak

推荐答案

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
       
        string textBoxID = ((TextBox)row.FindControl("IDTextBox")).Text;
        SqlTransaction st = null;
        try
        {
            conn.Open();
            st = conn.BeginTransaction();
            SqlCommand command1= new SqlCommand("Delete from tablename where MyID =''" + Convert.ToInt32(textBoxID) + "''", conn,st);
                   
            
            
            int num = command1.ExecuteNonQuery();
            
            
            if (num != 0)
            {
                
               
                   
                    st.Commit();
                    BindGrid();
               
                
            }
        }
        catch(Exception)
        {
            if (st == null)
                st.Rollback();
            
          
        }
        finally
        {
        conn.Close();
        }
        
    }

and GridView should look like this:

<pre><asp:GridView ID="GridView1"  runat="server"  AutoGenerateColumns="false" Font-Bold="True"
                Font-Size="Large" ShowFooter="true" AutoGenerateDeleteButton="True"   OnRowDeleting="GridView1_RowDeleting"  

                <Columns>

                 <asp:TemplateField HeaderText="Finding ID">
                        <ItemTemplate>
                            <asp:TextBox ID="IDTextBox" Width="198px"  runat="server" BackColor="White" BorderColor="White"
                                Font-Bold="True" Font-Size="Large"  Height="23px"  ForeColor="Black"></asp:TextBox>
                        </ItemTemplate>
                     <HeaderStyle ForeColor="Blue" />
                    </asp:TemplateField>








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

10-20 11:13