0

Hi everybody,

I am unable to get the index of the row which is selected by the user through checking a checkbox.
After getting the index I want to delete the row directly from the datagrid on that particular index.

Please help me out.Its urgent...


cheers,
sneha

4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by reach_yousuf
0

Hi... try this in your delete button click event

For Each gv2 As GridViewRow In gd.Rows
            Dim cb As CheckBox = gv2.FindControl("checkbox1")
            If cb IsNot Nothing AndAlso cb.Checked Then
 Dim sa As Integer = Convert.ToInt16(gd.DataKeys(gv2.RowIndex).Value)
com = New OleDb.OleDbCommand
           com.CommandText = "delete from stud where regNum=" & sa
                com.Connection = con
                con.Open()
                com.ExecuteNonQuery()

all the best

0
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType
!= DataControlRowType.Footer && e.Row.RowType !=
DataControlRowType.Pager)
{
LinkButton lblTagLink =new LinkButton();
lblTagLink = (LinkButton)e.Row.FindControl("btnAddTags");
lblTagLink.CommandArgument= e.Row.RowIndex.ToString();
}
}







So got the row index in

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{

int RowIndex_ = int.Parse(e.CommandArgument.ToString());
Label lblMediaID_ =
(Label)GridView1.Rows[RowIndex_].FindControl("lblMedia_ID");

}
0

Hi there

Try this

Aspx

<asp:GridView ID="gvBranchInfo" runat="server" BackColor="linen" AutoGenerateColumns="False"
                                                    DataKeyNames="ID" CellPadding="2" ForeColor="black" Font-Names="verdana">
                                                    <HeaderStyle BackColor="#CE3000" Font-Bold="True" Font-Size="10pt" ForeColor="white" />
                                                    <RowStyle BackColor="Linen" Font-Size="10pt" />
                                                    <AlternatingRowStyle BackColor="LightGrey" />
                                                    <Columns>
                                                        <asp:CommandField ButtonType="Button" ShowEditButton="false" ShowDeleteButton="True" />
                                                        <asp:BoundField HeaderText="S.No." DataField="ID" ReadOnly="True" />
                                                        <asp:BoundField HeaderText="BranchName" DataField="BranchName" />
                                                        <asp:BoundField HeaderText="CompanyID" DataField="CompanyID" ReadOnly="True" Visible="False" />
                                                        <asp:BoundField HeaderText="CompanyName" DataField="CompanyName" ReadOnly="True"
                                                            Visible="False" />
                                                        <asp:BoundField HeaderText="CategoryID" DataField="CategoryID" ReadOnly="True" Visible="False" />
                                                        <asp:BoundField HeaderText="CategoryName" DataField="CategoryName" ReadOnly="True" />
                                                        <asp:BoundField HeaderText="LoginEmail" DataField="LoginEmail" ReadOnly="True" Visible="False" />
                                                        <asp:BoundField HeaderText="Address" DataField="Address" />
                                                        <asp:BoundField HeaderText="Street" DataField="Street" />
                                                        <asp:BoundField HeaderText="PostBoxNo" DataField="PostBoxNo" />
                                                        <asp:BoundField HeaderText="Zipcode" DataField="Zipcode" />
                                                        <asp:BoundField HeaderText="CityID" DataField="CityID" ReadOnly="True" Visible="False" />
                                                        <asp:BoundField HeaderText="City Name" DataField="CityName" ReadOnly="True" />
                                                        <asp:BoundField HeaderText="CountryID" DataField="CountryID" Visible="False" />
                                                        <asp:BoundField HeaderText="Country Name" DataField="CountryName" ReadOnly="True" />
                                                        <asp:BoundField HeaderText="Branch Email" DataField="Email" />
                                                        <asp:BoundField HeaderText="Web Address" DataField="WebAddress" />
                                                        <asp:BoundField HeaderText="Created Date" DataField="CreatedDate" ReadOnly="True"
                                                            Visible="False" />
                                                    </Columns>
                                                </asp:GridView>

aspx.vb

Protected Sub gvBranchInfo_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvBranchInfo.RowCancelingEdit
        Dim dt As New DataTable
        dt = Session("BranchDT")
        gvBranchInfo.EditIndex = -1
        BindGrid(dt)
    End Sub
    Protected Sub gvBranchInfo_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvBranchInfo.RowDeleting
        Dim dt As New DataTable
        dt = Session("BranchDT")
        dt.Rows(e.RowIndex).Delete()
        BindGrid(dt)
    End Sub
    Protected Sub gvBranchInfo_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvBranchInfo.RowEditing
        Dim dt As New DataTable
        dt = Session("BranchDT")
        gvBranchInfo.EditIndex = e.NewEditIndex
        BindGrid(dt)
    End Sub
    Protected Sub gvBranchInfo_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvBranchInfo.RowUpdating
        ''TO ADD UPDATE METHOD TO DATA TABLE AND GRIDVIEW

        Try
            Dim dt As New DataTable
            dt = CType(Session("BranchDT"), DataTable)
            Dim drw As DataRow
            If Not IsNothing(dt) Then
                drw = dt.Rows(dt.DefaultView.Count - 1)
            End If
            drw = dt.DefaultView(e.RowIndex).Row
            drw("BranchName") = gvBranchInfo.Rows(e.RowIndex).Cells(2).Text
            drw("CompanyID") = gvBranchInfo.Rows(e.RowIndex).Cells(3).Text
            drw("CompanyName") = gvBranchInfo.Rows(e.RowIndex).Cells(4).Text
            drw("CategoryID") = gvBranchInfo.Rows(e.RowIndex).Cells(5).Text
            drw("CategoryName") = gvBranchInfo.Rows(e.RowIndex).Cells(6).Text
            drw("LoginEmail") = gvBranchInfo.Rows(e.RowIndex).Cells(7).Text
            drw("Address") = gvBranchInfo.Rows(e.RowIndex).Cells(8).Text
            drw("Street") = gvBranchInfo.Rows(e.RowIndex).Cells(9).Text
            drw("PostBoxNo") = gvBranchInfo.Rows(e.RowIndex).Cells(10).Text
            drw("Zipcode") = gvBranchInfo.Rows(e.RowIndex).Cells(11).Text
            drw("CityID") = gvBranchInfo.Rows(e.RowIndex).Cells(12).Text
            drw("CityName") = gvBranchInfo.Rows(e.RowIndex).Cells(13).Text
            drw("CountryID") = gvBranchInfo.Rows(e.RowIndex).Cells(14).Text
            drw("CountryName") = gvBranchInfo.Rows(e.RowIndex).Cells(15).Text
            drw("Telephone") = gvBranchInfo.Rows(e.RowIndex).Cells(16).Text
            drw("Email") = gvBranchInfo.Rows(e.RowIndex).Cells(17).Text
            drw("WebAddress") = gvBranchInfo.Rows(e.RowIndex).Cells(18).Text
            drw("CreatedDate") = Format(Date.Today, "yyyy-MM-dd")

            dt.AcceptChanges()

            Session("BranchDT") = Nothing
            Session("BranchDT") = dt

            gvBranchInfo.EditIndex = -1

            gvBranchInfo.DataSource = dt
            gvBranchInfo.DataBind()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

Mark as solved if it helps you!!!

Happy programming!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.