Friday, 19 October 2012

insert update delete with gridview in asp.net with c#


                                 
---------------------------------------------------------------------------------

    ******************    GridView Best CODES   *************************
----------------------------------------------------------------------------------



------------------------------------------------
RowDeleting
-------------------------------------------------
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            id = GridView1.Rows[e.RowIndex].Cells[1].Text;
            con.update_data("delete from Seminar_table where Semi_ID=" + id + "");
            Grid_Bind();
        }
        catch (Exception ex)
        {
            RMG.Functions.MsgBox(ex.Message);
        }
    }



-------------------------------------------------
RowEditing:------->
------------------------------------------------

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            GridView1.EditIndex = e.NewEditIndex;
            Grid_Bind();
        }
        catch (Exception ex)
        {
            RMG.Functions.MsgBox(ex.Message);
        }
    }
------------------------------------------------

RowUpdating:------->
------------------------------------------------
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
           string  id = GridView1.Rows[e.RowIndex].Cells[1].Text;

        try
        {
            string str1, str2, str3;
            str1 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            str2 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            str3 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
            string id = GridView1.Rows[e.RowIndex].Cells[1].Text;

            con.update_data("update Seminar_table set Semi_Name='" + str1 + "',Semi_Desc='" + str2 + "', Semi_Date='" + str3 + "' where Semi_ID='" + id + "'");
            GridView1.EditIndex = -1;
        }
        catch (Exception ex)
        {
            RMG.Functions.MsgBox(ex.Message);
        }
    }
---------------------------------------------------
Row Canceling:--->
------------------------------------------------
           
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        try
        {
            GridView1.EditIndex = -1;
            Grid_Bind();
        }
        catch (Exception ex)
        {
            RMG.Functions.MsgBox(ex.Message);
        }
    }
-----------------------------------------------------

Row SELECTing:--->
-----------------------------------------------------


 protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string id = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;

   }


---------------------------------------------------------------
Row Editing Disabling Coding:------>
-----------------------------------------------------
protected void GridView_ShowAll_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            GridView_ShowAll.EditIndex = e.NewEditIndex;
            GridView_Show();
            GridView_ShowAll.Rows[e.NewEditIndex].Cells[1].Enabled = false;
            GridView_ShowAll.Rows[e.NewEditIndex].Cells[2].Enabled = false;
        }
        catch (Exception ex)
        {
            RMG.Functions.MsgBox(ex.Message);
        }
    }
---------------------------------------------------------------
Inserting Data in Table Coding:------>
-----------------------------------------------------

protected void  btn_Save_Click(object sender, EventArgs e)
    {
      string ename,edetail,date;
      int eamt;
       ename=txt_exname.Text;
       edetail=txt_exdetail.Text;
       eamt =Convert.ToInt32(txt_examt.Text);
       date=txt_Date.Text;
       con.insert_data("INSERT INTO Expences_Detail(E_Name, E_Detail, E_amt, E_Date)VALUES ('"+ename+"', '"+edetail+"', "+eamt+", "+date+")");
       RMG.Functions.MsgBox("Expenecs Detail hasbeen Inserted  !!!!!");

       txt_exname.Text="";
       txt_exdetail.Text="";
       txt_examt.Text = "";
       txt_Date.Text="";
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------

Updation With CheckBox-------->(--cs--)
------------------------------------------------------------------------

 protected void btn_cancel_Click(object sender, EventArgs e)
        {
            if (GridView1.Rows.Count > 0)
            {
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[11].FindControl("chk");
                    string id = GridView1.Rows[i].Cells[2].Text;
                    if (chk.Checked == true)
                    {
                        con.update_data("update Payout set Flag='Y',cheque=0 where MainID='"+id+"'");
                    }
                }
            }
        }

-------------------------------------------------------------------------------------------

  (----aspx-----)
---------------------------------------------------------------------------------------
 <asp:TemplateField>
         <HeaderTemplate>
          <asp:CheckBox id="chkheader" AutoPostBack="true"  runat="server" OnCheckedChanged="chkheader_CheckedChanged" />
                                                   
           </HeaderTemplate>
           <ItemTemplate>
           <asp:CheckBox ID="chk" runat="server" />
           </ItemTemplate>
    </asp:TemplateField>

-------------------------------------------------------------------------------------


 subGroup.SubGroupName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
  subGroup.AcMGroupID = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text);

No comments:

Post a Comment