Click here to Skip to main content
15,920,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to know how to modify data in datagrid and it save automatically in database,
plzz help me,
Posted

C#
DataSet ds;
OleDbDataAdapter dataAdapter;
void ReadData()
    {
        this.ds = new DataSet();
        string connString = "CONNICTION STRING GOES HERE";
        this.dataAdapter = new OleDbDataAdapter("QUERY GOES HERE", connString);
        this.dataAdapter.Fill(this.ds, "TABLE1");
        this.ds.AcceptChanges();
        //set the table as the datasource for the grid in order to show that data in the grid
        this.dataGridView1.DataSource = ds.DefaultViewManager;
    }

    void SaveData()
    {
        DataSet changes = this.ds.GetChanges();
        if (changes != null)
        {
            //Data has changes. 
            //use update method in the adapter. it should update your datasource
            int updatedRows = this.dataAdapter.Update(changes);
            this.ds.AcceptChanges();
        }
    }
 
Share this answer
 
Comments
M-osab 8-Aug-14 11:23am    
It does not work
no error but nothing happen
Here is the example code for that

ASPX:

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
            <Columns>
                <asp:TemplateField HeaderText="EmpNO">
                    <ItemTemplate>
                        <%#Eval("Eno") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEno" runat="server" Text='<%#Eval("Eno") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtIEno" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EName">
                    <ItemTemplate>
                        <%#Eval("Ename") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEname" runat="server" Text='<%#Eval("Ename") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtIEname" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Salary">
                    <ItemTemplate>
                        <%#Eval("salary") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtloc" runat="server" Text='<%#Eval("salary") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtIloc" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <FooterTemplate>
                        <asp:Button ID="btnSave" OnCommand="insert" Text="save" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="true" />
                <asp:CommandField ShowDeleteButton="true" />
            </Columns>
        </asp:GridView>



.CS Code:

protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection("Your Connection string");
if (!IsPostBack)
{
getdata();
}
}
SqlConnection con;
SqlCommand cmd;
private void getdata()
{
SqlDataAdapter da = new SqlDataAdapter("select * from empdet", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
getdata();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox t1, t2, t3;
t1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEno");
t2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEname");
t3 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtloc");
con.Open();
cmd = new SqlCommand("update empdet set eno=" + t1.Text + ",ename='" + t2.Text + "',salary=" + t3.Text + " where eno="+t1.Text+"", con);
cmd.ExecuteNonQuery();
con.Close();
}
public void insert(object s, EventArgs e)
{
TextBox t1, t2, t3;
t1 = (TextBox)GridView1.FooterRow.FindControl("txtIEno");
t2 = (TextBox)GridView1.FooterRow.FindControl("txtIEname");
t3 = (TextBox)GridView1.FooterRow.FindControl("txtIloc");
SqlConnection con = new SqlConnection("uid=sa; password=secret; data source=IN105-D\\SQL2012; database=SampleDB");
con.Open();
cmd = new SqlCommand("INSERT INTO empdet VALUES(" + t1.Text + ",'" + t2.Text + "'," + t3.Text + ")", con);
cmd.ExecuteNonQuery();
getdata();
con.Close();
}
 
Share this answer
 
Comments
M-osab 8-Aug-14 11:25am    
this code is for a site web (asp.net) I'm just working with wpf,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900