Hi all,
Please help me in solving following query:
I want to update table at backend in database throughmakeing changes in
datagridview.
What I want is, when User makes changes in datagridview and clicks save
or update button, the changes which are made in datagridview should get
reflect in table in databse.
For ex:
I am having "Datagridview", "Edit" and "Save" button on WinForm and
table "EmpDetails" at backend in databse. I am filling "Datagridview"
with table "EmpDetails". When user clicked "Edit" button he should
allowed to make changes in "Datagridview". After making changes in
"Datagridview" when user clicked "Save" button, whichever changes made
in datagridview should get reflect at backend in table "EmpDetails".
HELP ME ITS URGENT
Loading
Sam HobbsPosted Jul 12, 2010, 4:20 PM
Sam HobbsPosted Jul 15, 2010, 11:41 AM
Aaditya GanesanPosted Jul 15, 2010, 10:45 AM
Sam HobbsPosted Jul 14, 2010, 11:56 PM
If the DataGridView is bound to the prodspec table then you can use the BindingSource.Filter Property of the DataGridView's BindingSource to filter the data shown in the DataGridView so that only the selected product is shown.
Aaditya GanesanPosted Jul 14, 2010, 7:10 PM
i have a combo box that list out all the prodcode and it is connected to the database.
I use this combo box to generate a gridview, So when i choose a prodcode I get a gridview of that particular prodcode with all the properties of the prodcode(just one row of data).
This is done with a help of select statement and it works perfectly and then if the user modifies the properties or the row generated in the gridview and then on pressing the update button, I want it to update, but it ain't happenin..
I hope I am clear now
Thank you
Sam HobbsPosted Jul 14, 2010, 6:53 PM
So what I am saying is that it is not clear what the data is that you need to show and update and how the form works. You say you have a ComboBox with prodcode but I don't know how it is related to the EmpDetails table. I think we need more information about the data and what you are using to view and update it.
Aaditya GanesanPosted Jul 14, 2010, 5:34 PM
try
{
string connStr = @"Data Source=MPMSQLDEV;Initial Catalog=prodcn;Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlDataAdapter dAd = null;
DataSet dSet = new DataSet();
string strSql = "Select * FROM prodspec where prodcode='" + prodcode.Text + "'";
dAd = new SqlDataAdapter(strSql, conn);
dAd.Fill(dSet, "prodspec");
prodspecDataGridView.DataSource = dSet.Tables[0];
prodspecDataGridView.DataSource = dSet.Tables["prodspec"].DefaultView;
prodspecDataGridView.Visible = true;
button4.Visible = true;
}
catch (Exception ee)
{
MessageBox.Show("no entry");
}
}
private void closegrid_Click(object sender, EventArgs e)
{
prodspecDataGridView.Visible = false;
button4.Visible = false;
}
private void modify_Click_1(object sender, EventArgs e)
{
DataSet dSet = new DataSet();
SqlDataAdapter dAd = new SqlDataAdapter();
dAd.Update(dSet, "prodspec");
dSet.Tables["prodspec"].AcceptChanges();
// DataGridViewRow item = new DataGridViewRow();
// prodspecDataGridView.AllowUserToAddRows = true;
//item.CreateCells(prodspecDataGridView); (I tried with these comment lines also but it is not working)
My prob is with your method I am able to update the database but wen I chose a single prodcode from the combo box in the form, the gridview populates with a single row of that of the prodcode. but when I try updating by either the save button in toolbar or the modify button that i have coded it does not updates the database and shows an exception in the latter case Update unable to find TableMapping['prodspec'] or DataTable 'prodspec'.
plz help me on how to select a single row from a combo box in the form or from the datagridview so that i get the corresponding values of the product and I can update the gridview and the database...
hope I am clear, vry urgent
Adi
Aaditya GanesanPosted Jul 14, 2010, 10:03 AM
Aaditya GanesanPosted Jul 14, 2010, 9:10 AM
thank you so much
Sam HobbsPosted Jul 13, 2010, 11:48 PM
What is the select button supposed to do?
Aaditya GanesanPosted Jul 13, 2010, 9:03 PM
namespace UpdateDataWindowsFormDataGridView
{
public partial class Form1 : Form
{
private SqlDataAdapter da;
private DataTable dt;
public Form1()
{
InitializeComponent();
// Configure display characteristics of the data grid view
dataGridView.Anchor = AnchorStyles.Left | AnchorStyles.Right |
AnchorStyles.Top | AnchorStyles.Bottom;
this.Width = 500;
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
// Set the data source of the data grid view
dataGridView.DataSource = LoadData();
}
private DataTable LoadData()
{
string sqlConnectString = @"my connetion";
string sqlSelect = "SELECT Id, IntField, StringField FROM DataGridView";
// Create a data adapter and command builder
da = new SqlDataAdapter(sqlSelect, sqlConnectString);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
// Load a DataTable with schema and data from table DataGridView
dt = new DataTable();
da.FillSchema(dt, SchemaType.Source);
da.Fill(dt);
return dt;
}
private void saveButton_Click(object sender, EventArgs e)
{
da.Update(dt);
MessageBox.Show("Changes saved.", "DataGridView",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
This is not my code but I just did exactly like this but on a extra note I have added a button which selects the value in the gridview…nw I want a button for just updating the gridview… wen I use this code the database remains unchanged(nothing happens)…. For the second button I have added the SQL dataadaptor and datatable publically hw t is in the prog but with a different object instance(name)..
This code is workin if I create a dummy form with just one gridview and a button, but not wen 2 buttons (one for select and 1 for update ).. is there any other way to do this..