Tuesday 23 July 2013

Row Command Event in Grid View



<asp:GridView ID="GridView1" runat="server"
                                        onrowcommand="GridView1_RowCommand" CellPadding="1" CellSpacing="2"
                                        GridLines="None" Width="850px">
                                        <Columns>
                                        <asp:TemplateField>
                                        <HeaderTemplate>Delete</HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Button ID="Button1" runat="server" Text="Delete" CommandName="DeleteRow"
                                                CommandArgument='<%#Eval("Message ID") %>' Height="24px" Width="81px" />
                                            <asp:ConfirmButtonExtender ID="ConfirmButtonExtender1" runat="server" TargetControlID="Button1" ConfirmText="You want to delete this Message.">
                                            </asp:ConfirmButtonExtender>
                                            </ItemTemplate>
                                           
                                            </asp:TemplateField>
                                             
                                              <asp:TemplateField>
                                       <HeaderTemplate>Read</HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:Button ID="Button2" runat="server" Text="Read" CommandName="Select"
                                                    CommandArgument='<%#Eval("Message ID") %>' Height="25px" Width="80px" />
                                            </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                        <HeaderStyle BackColor="Blue" ForeColor="White" Height="40px"
                                            HorizontalAlign="Center" />
                                        <RowStyle Height="30px" HorizontalAlign="Center" />
                                    </asp:GridView>

Command Argument contail same value as you are specifying in select command
if u rename any column by as keyword in select command then command argument will use renamed name  for ex original data column is Message_Id  you retrieve it as Message ID then command argument will use Message ID not Message_Id.


in code file

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
           
           
            if (e.CommandName == "DeleteRow")
            {
                int index =Convert.ToInt32(e.CommandArgument);
                //GridViewRow row = GridView1.Rows[index];
                //GridView1.DeleteRow(index);
                con = new MySqlConnection(ss);
                con.Open();
                cmd = new MySqlCommand("delete from message where Message_Id=@mid", con);
                cmd.Parameters.Add(new MySqlParameter("mid", index));
                cmd.ExecuteNonQuery();
                con.Close();
               
            }


        }

No comments:

Post a Comment