none
Gridview Update nasıl oluyor anlamadım RRS feed

  • Soru

  • Merhaba gridview  insert ve update  nasıl yapılır onu öğrenmeye çalışıyorum.Internetten bir örnek buldum Az çok anladım  fakat bir yere kafam pek basmadı.Gridview de rowcommand diye bir event var orda şöyle bir kontrol yapılıyor.

    if(e.CommandName.Equals("ADD"))

    Ama update için yok onun yerine Row_Updating eventi var .Adam update kodlarını burda yazmış .Benim anlamadığı nokta burası .. ben buttona tıkladığımda nasıl update oluyor.Yada şöyle anlatıyım mesela şöyle birşey yok(e.CommandName.Equals("Update")) Neden diğer buttonlara tıklayınca değilde Update butonuna basınca tetikleniyor

        
             <asp:GridView ID="gvEmployeeDetails" runat="server" Width="600px" 
                        AutoGenerateColumns="false" ShowFooter="true"
                        onrowcommand="gvEmployeeDetails_RowCommand" 
                        onrowdeleting="gvEmployeeDetails_RowDeleting" 
                        onrowupdating="gvEmployeeDetails_RowUpdating" 
                        onrowcancelingedit="gvEmployeeDetails_RowCancelingEdit" 
                        onrowediting="gvEmployeeDetails_RowEditing"
                        HeaderStyle-BackColor="#4D4D4D"
                        HeaderStyle-ForeColor="White">
                    <Columns>            
                        <asp:TemplateField HeaderText="Employee ID">
                            <ItemTemplate>
                                <asp:Label ID="lblEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "empid") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>            
                                <asp:Label ID="lblEditEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "empid") %>'></asp:Label>            
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtAddEmpID" runat="server" Width="100px"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "name") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>            
                                <asp:TextBox ID="txtEditName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "name") %>'></asp:TextBox>            
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtAddName" runat="server" Width="100px"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="Designation">
                            <ItemTemplate>
                                <asp:Label ID="lblDesignation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "designation") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>            
                                <asp:TextBox ID="txtEditDesignation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "designation") %>'></asp:TextBox>            
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtAddDesignation" runat="server" Width="150px"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="City">
                            <ItemTemplate>
                                <asp:Label ID="lblCity" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "city") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>            
                                <asp:TextBox ID="txtEditCity" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "city") %>'></asp:TextBox>            
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtAddCity" runat="server" Width="80px"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="Country">
                            <ItemTemplate>
                                <asp:Label ID="lblCountry" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "country") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>            
                                <asp:TextBox ID="txtEditCountry" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "country") %>'></asp:TextBox>            
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtAddCountry" runat="server" Width="80px"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="Action">
                            <ItemTemplate>
                               <asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" ImageUrl="~/Images/icon-edit.png" Height="32px" Width="32px"/>
                               <asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/Delete.png"/>
                            </ItemTemplate>
                            <EditItemTemplate>
                               <asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="~/Images/icon-update.png"/>
                               <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/icon-Cancel.png"/>
                            </EditItemTemplate>
                            <FooterTemplate>
                               <asp:LinkButton ID="lbtnAdd" runat="server" CommandName="ADD" Text="Add" Width="80px"></asp:LinkButton> 
                            </FooterTemplate>
                        </asp:TemplateField>
                        
                    </Columns>            
                </asp:GridView>
    
    
    
    
    
    
    protected void BindData()
        {       
            DataSet ds = new DataSet();
            DataTable FromTable = new DataTable();
            try
            {
                conn.Open();
                string cmdstr = "Select * from EmployeeDetails";
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                SqlDataAdapter adp = new SqlDataAdapter(cmd);           
                adp.Fill(ds);
                cmd.ExecuteNonQuery();
                FromTable = ds.Tables[0];            
                if (FromTable.Rows.Count > 0)
                {
                    gvEmployeeDetails.DataSource = FromTable;
                    gvEmployeeDetails.DataBind();
                }
                else
                {
                    FromTable.Rows.Add(FromTable.NewRow());
                    gvEmployeeDetails.DataSource = FromTable;
                    gvEmployeeDetails.DataBind();
                    int TotalColumns = gvEmployeeDetails.Rows[0].Cells.Count;
                    gvEmployeeDetails.Rows[0].Cells.Clear();
                    gvEmployeeDetails.Rows[0].Cells.Add(new TableCell());
                    gvEmployeeDetails.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                    gvEmployeeDetails.Rows[0].Cells[0].Text = "No records Found";
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                ds.Dispose();
                conn.Close();
            }
        }
    
        protected void gvEmployeeDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Label lblEmpID = (Label)gvEmployeeDetails.Rows[e.RowIndex].FindControl("lblEmpID");
           
            conn.Open();
            string cmdstr = "delete from EmployeeDetails where empid=@empid";
            SqlCommand cmd = new SqlCommand(cmdstr, conn);
            cmd.Parameters.AddWithValue("@empid", lblEmpID.Text);
            cmd.ExecuteNonQuery();
            conn.Close();
            BindData();
    
        }
        protected void gvEmployeeDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("ADD"))
            {
                TextBox txtAddEmpID = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddEmpID");
                TextBox txtAddName = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddName");
                TextBox txtAddDesignation = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddDesignation");
                TextBox txtAddCity = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddCity");
                TextBox txtAddCountry = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddCountry");
    
    
                conn.Open();
                string cmdstr = "insert  EmployeeDetails values(@empid,@name,@designation,@city,@country)";
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                cmd.Parameters.AddWithValue("@empid", txtAddEmpID.Text);
                cmd.Parameters.AddWithValue("@name", txtAddName.Text);
                cmd.Parameters.AddWithValue("@designation", txtAddDesignation.Text);
                cmd.Parameters.AddWithValue("@city", txtAddCity.Text);
                cmd.Parameters.AddWithValue("@country", txtAddCountry.Text);
                cmd.ExecuteNonQuery();
                conn.Close();
                BindData();
            }       
        }
        protected void gvEmployeeDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Label lblEditEmpID = (Label)gvEmployeeDetails.Rows[e.RowIndex].FindControl("lblEditEmpID");
            TextBox txtEditName = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditName");
            TextBox txtEditDesignation = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditDesignation");
            TextBox txtEditCity = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditCity");
            TextBox txtEditCountry = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditCountry");
    
            conn.Open();
            string cmdstr = "update EmployeeDetails set name=@name,designation=@designation,city=@city,country=@country where empid=@empid";
            SqlCommand cmd = new SqlCommand(cmdstr, conn);
            cmd.Parameters.AddWithValue("@empid", lblEditEmpID.Text);
            cmd.Parameters.AddWithValue("@name", txtEditName.Text);
            cmd.Parameters.AddWithValue("@designation", txtEditDesignation.Text);
            cmd.Parameters.AddWithValue("@city", txtEditCity.Text);
            cmd.Parameters.AddWithValue("@country", txtEditCountry.Text);
            cmd.ExecuteNonQuery();
            conn.Close();
            gvEmployeeDetails.EditIndex = -1;
            BindData();
    
        }

    24 Haziran 2014 Salı 15:07