select,edit,delete,update data in datagridview control[c#]

Rate It (1)

Last post 06-13-2008 1:46 AM by geeta_ravikanti. 12 replies.

Sort Posts:

  • select,edit,delete,update data in datagridview control[c#]

    06-10-2008, 1:40 AM

    Hi friends,

    In my program I have to edit,update,delete of data in datagridview control,

    i.e I have to bind the datagridview  with databse,what ever the operation I done at database side the same things I have to do in datagridview control

    Pleae send me any sample codes,or sample links

    and I really very thankful to one and all this forum is very helping in doing program beacause I don't the .net but I am doing the program in .net because of all the members cooperation in this forum

     

     

    thanks & regards

    Geeta 

     

  • Re: select,edit,delete,update data in datagridview control[c#]

    06-10-2008, 2:45 AM
    • Participant
      1,465 point Participant
    • Rizwan328
    • Member since 05-25-2006, 10:26 AM
    • Dubai, UAE
    • Posts 349
    If this post helps you, please mark it as Answer.



    Cheers,
    Muhammad Rizwan Javed
    Sr .Net Developer
    Aim 168, Dubai, UAE.
  • Re: select,edit,delete,update data in datagridview control[c#]

    06-10-2008, 3:24 AM
    • Member
      17 point Member
    • Ramganeshv
    • Member since 04-18-2008, 4:59 AM
    • Posts 50

     hi

    Refer the below link which contain simple code to do ur required stuff

    http://www.codersource.net/asp_net_datagrid_part1_azam.html

    http://www.codersource.net/asp_net_datagrid_part2_azam.html 

     

     

     

  • Re: select,edit,delete,update data in datagridview control[c#]

    06-10-2008, 4:41 AM
  • Re: select,edit,delete,update data in datagridview control[c#]

    06-10-2008, 4:56 AM
    • Participant
      1,041 point Participant
    • sbakshi
    • Member since 05-21-2008, 9:25 AM
    • Posts 183

     Hi,

     Download source code from:

    http://aspalliance.com/1125_Dynamically_Templated_GridView_with_Edit_Delete_and_Insert_Options.6
     Hope it helps you

    Warm Regards:
    Sitanshu

    Please "Mark as Answer" if my post sort your problem

    Blog: http://sharp-architecture.blogspot.com/
  • Re: select,edit,delete,update data in datagridview control[c#]

    06-10-2008, 9:21 AM

     thanq for the reply

    with the help of all you I am able to binding the data with gridview  and I put the edit ,delete,update column with command from gridview properties

    but after running the program when I click on the edit link its throwing the exception

    and I changed the proerty readOnly=true also

    but after clicking on the edit link its I unable to see the textbox for updating

    please help me inthis

     

    thanks

    Geeta 

  • Re: select,edit,delete,update data in datagridview control[c#]

    06-10-2008, 9:59 AM

    I got update and cancel link after clicking the edit link and I am able to see the textboxes for editng the data but what I want is I want to update the data i.e I changed the the existing data of employeeName

    for e.g I have one field empName="geeta" here I changed with this "kavita" and after that I cliked on the update link

    but its not updating the data when I checked again in the database

    I wrote the code like this

    protected void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            GridViewRow row = myGridView.Rows[e.RowIndex];

            if (row != null)
            {

                TextBox t = row.FindControl("TextBox1") as TextBox;

                if (t != null)
                {

                    Response.Write("The Text Entered is" + t.Text);

                }

            }
        }

     

    So please tell me anyone how to update the data in the existing column

     

    Thanks

    Geeta 

     

  • Re: select,edit,delete,update data in datagridview control[c#]

    06-11-2008, 2:12 AM

     Hi Friends,

    I wrote the code again for updating the data in the datagridview like this

     protected void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {


            conn.Open();
            SqlDataAdapter ad1 = new SqlDataAdapter("SELECT * FROM EmployeeTable", conn);
            DataSet ds1 = new DataSet();

            ad1.Update(ds1, "EmployeeTable");
            conn.Close();

    but tis throwing the exception like this after clicking on the update link

    Update unable to find TableMapping['EmployeeTable'] or DataTable 'EmployeeTable'.

    SO where to change the code please help me in this.....

     

  • Re: select,edit,delete,update data in datagridview control[c#]

    06-12-2008, 3:22 AM
    Answer

    Hi,

    It's incorrect to use SqlDataAdapter.Update. In RowUpdating event, you need to update the data in Database with update sql statement.

     

        protected void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id=// To find key of current row. e.g: myGridView.DataKeys[e.RowIndex].Value.ToString();
            string text=//To find the text value from the current row.
            conn.Open();
            SqlCommand UpdateCmd = new SqlCommand("update EmployeeTable set text='"+text+"' where id="+id,UpdateCon);
            UpdateCmd.ExecuteNonQuery();
            conn.Close();
    
                GridView1.EditIndex = -1; //After updating, please set EditIndex and re-bind the GridView.
                BindGridView(); }
    You can check the complete functions from the below link:
    http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx
    Hope it helps.
     
     

    Vince Xu
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: select,edit,delete,update data in datagridview control[c#]

    06-12-2008, 3:29 AM
    • Contributor
      2,043 point Contributor
    • kalyan1982
    • Member since 04-10-2008, 8:28 AM
    • Johannesburg, South Africa
    • Posts 353

    Hi

    aspx code:

    <%@ Import Namespace="System.Data.SqlClient" %>

    <%@ Import Namespace="System.Data" %>

    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="DFADetainees.WebForm2"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <HTML>

    <HEAD>

    <title>WebForm2</title>

    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">

    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">

    <meta name="vs_defaultClientScript" content="JavaScript">

    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

    </HEAD>

    <body MS_POSITIONING="GridLayout">

    <form id="Form1" method="post" runat="server">

    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 96px"

    runat="server" Width="376px" Height="173px">

    <Columns>

    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit" CancelText="Cancel"

    EditText="Edit"></asp:EditCommandColumn>

    </Columns>

    </asp:DataGrid>&nbsp;

    </form>

    </body>

    </HTML>

     

    aspx.vb code:

    Imports System.Data.SqlClient

    Public Class WebForm2

    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.

    'Do not delete or move it.

    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

    'CODEGEN: This method call is required by the Web Form Designer

    'Do not modify it using the code editor.

    InitializeComponent()

    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    If Not Page.IsPostBack Then

    BindData()

    End If

    End Sub

    Public Sub BindData()

    Dim myDataSet As New DataSet

    Dim myDataSetCommand As SqlClient.SqlDataAdapter

    Dim str As String = "server=localhost;database=pubs;uid=sa;pwd=ecom;"

    Dim con As New SqlClient.SqlConnection(str)myDataSetCommand = New SqlClient.SqlDataAdapter("SELECT * FROM sales", con)

    myDataSetCommand.Fill(myDataSet, "sales")

    DataGrid1.DataSource = myDataSet.Tables("sales").DefaultView

    DataGrid1.DataBind()

    End Sub

    Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand

    DataGrid1.EditItemIndex = e.Item.ItemIndex

    BindData()

    End Sub

    Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand

    DataGrid1.EditItemIndex = -1

    BindData()

    End Sub

    Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand

    Dim txtstore As TextBox = e.Item.Cells(1).Controls(0)

    Dim txtorder As TextBox = e.Item.Cells(2).Controls(0)

    Dim txtdate As TextBox = e.Item.Cells(3).Controls(0)

    Dim txtq As TextBox = e.Item.Cells(4).Controls(0)

    Dim txtpay As TextBox = e.Item.Cells(5).Controls(0)

    Dim txttitle As TextBox = e.Item.Cells(6).Controls(0)

    Dim strUpdateStmt As String

    strUpdateStmt = "Update sales set ord_num='" & txtorder.Text & "' where title_id='" & txttitle.Text & "'"

    Dim str As String = "server=localhost;database=pubs;uid=sa;pwd=ecom;"

    Dim con As New SqlClient.SqlConnection(str)

    Dim myCommand As New SqlCommand(strUpdateStmt, con)

    con.Open()

    myCommand.ExecuteNonQuery()

    con.Close()

    DataGrid1.EditItemIndex = -1

    BindData()

    End Sub

    End Class

    Kalyan Krishna.B
    (Web Developer)

    Email: kalyandotnetdeveloper@gmail.com
  • Re: select,edit,delete,update data in datagridview control[c#]

    06-12-2008, 6:25 AM

     

    thanQ very much for the reply 

     
    Sorry,this time I got another exception

    actually I want update one row in the database and not total fields some of the fields I want to update

      In mygirdview I have the information like this

              empId      userName       empName      gender      address     pwd      loginstatus    edit  1                 geeta                geeta            f                hjghg     ghh        0

    so here I want to change the userName with kavita

    after clicking the edit button it will come like this

     

            empId      userName       empName      gender      address     pwd      loginstatus   update cancel  1               kavita               geeta            f               hjghg     ghh        0

    after that I clicked on the update link

    but now I got another exception

      

    I wrote the code lke following

    protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                BindData();
               
            }

        }
       

        public void BindData()
        {

           
            conn.Open();
            SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM EmployeeTable", conn);

            DataSet ds = new DataSet();

            ad.Fill(ds, "EmployeeTable");
            conn.Close();
        
            myGridView.DataSource = ds;

            myGridView.DataBind();
           

        }

        protected void myGridView_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)
        {

            myGridView.EditIndex = -1;

            BindData();
         
         }

        protected void updateRow(object sender, GridViewUpdateEventArgs e)
        {


              string id = myGridView.DataKeys[e.RowIndex].Value.ToString();
            TextBox txtName = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtName");
            conn.Open();
            SqlCommand UpdateCmd = new SqlCommand("update EmployeeTable set text='" + txtName + "' where empId=" + id,conn);
            UpdateCmd.ExecuteNonQuery();
            conn.Close();

            myGridView.EditIndex = -1; //After updating, please set EditIndex and re-bind the GridView.
            BindData();
           


        }


       string id = myGridView.DataKeys[e.RowIndex].Value.ToString();here I am getting the exception i.e   Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    So where to change the code

    please help me in this..................... 

  • Re: select,edit,delete,update data in datagridview control[c#]

    06-12-2008, 10:33 PM

    Hi,

    You have to set the DataKeyNames property before you bound the gridview

    GridView1.DataKeyNames = new string[] { "Key" };

    Or set property DataKeyNames on HTML


    Vince Xu
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: select,edit,delete,update data in datagridview control[c#]

    06-13-2008, 1:46 AM
    Answer

    thanQ for the help, 

    what ever you said its not working in my program I am getting the previuos exception only

    but I did solve my program in another way with the help of my firnd

     

    Thanks & Regards

    Geeta 

     

Page 1 of 1 (13 items)