Hi
i'm new to C#, i have a datagrid that i populate at form load. I change the data via the datagrid view and then want to click an update button which will update the dataset. Going round in circles with this one
Regards
N00b
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kirtan PatelPosted Oct 13, 2009, 10:45 AM
Just Execute All the Code Again that is in Form Load Event
or make one Function of all the Code in Form Load and Call that Function after Updating /Inserting Data so Grid view Will Show New Records
PelPosted Oct 13, 2009, 8:35 AM
thank you for the reply, but what if i am adding a new record in the datagridview. I wanted to use the dataadapter update and sqlcmdbuilder options to generate the respective update, insert or delete commands.
Regards
Pel
Kirtan PatelPosted Oct 12, 2009, 3:58 PM
Here is What you want I have also included Project Files So you can take reference if you don't understand by just reading code :) download link is At bottom of post
don't forget to mark "Do you like this answer" please it will give me some credits :)
namespace WindowsFormsApplication17
{
public partial class Form1 : Form
{
DataSet ds;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand comm = new SqlCommand("Select * from Users",con);
SqlDataAdapter da = new SqlDataAdapter(comm);
ds = new DataSet();
da.Fill(ds, "vtable");
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
DataSet DataSetwithModifiedRows = ds.GetChanges();
DataTable dt = DataSetwithModifiedRows.Tables[0];
foreach (DataRow r in dt.Rows)
{
string Username = r[0].ToString();
string Password = r[1].ToString();
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand comm = new SqlCommand("update Users set Password=@password where username=@username", con);
comm.Parameters.AddWithValue("@Username", Username);
comm.Parameters.AddWithValue("@password", Password);
comm.ExecuteNonQuery();
con.Close();
}
MessageBox.Show("Data Updated!!");
}
}
}
Download Project Files
Roei BarPosted Oct 12, 2009, 3:45 PM
once you click on the updatebutton just call again the
grid.dataBind()
in order for it to get the new changes