Insert, Update, Delete with DataGridView Control in C# (Windows Application)? [Resolved]

Posted by Sudheep.grandhe under C# on 11/18/2011 | Points: 10 | Views : 110011 | Status : [Member] | Replies : 7
Hi,

Insert, Update, Delete with DataGridView Control in C# (Windows Application).can you please provide the sample code.please do the needful.

Best,
Sudheep.



Responses

Posted by: Blessyjees on: 11/18/2011 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi,
see this code
public partial class DataTrialForm : Form

{
private String connectionString = null;
private SqlConnection sqlConnection = null;
private SqlDataAdapter sqlDataAdapter = null;
private SqlCommandBuilder sqlCommandBuilder = null;
private DataTable dataTable = null;
private BindingSource bindingSource = null;
private String selectQueryString = null;

public DataTrialForm()
{
InitializeComponent();
}

private void DataTraiForm_Load(object sender, EventArgs e)
{

sqlConnection = new SqlConnection("data source=SEZ-WS-137\\SQLEXPRESS2008;initial catalog=AttendanceData;user id=sa;password=pass");
selectQueryString = "select top 20 * from Attendance order by punchtime asc";

sqlConnection.Open();

sqlDataAdapter = new SqlDataAdapter(selectQueryString, sqlConnection);
sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);

dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;

dataGridViewTrial.DataSource = bindingSource;

// to hide Identity column
dataGridViewTrial.Columns[0].Visible = false;
}

private void addUpadateButton_Click(object sender, EventArgs e)
{
try
{
sqlDataAdapter.Update(dataTable);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}

private void deleteButton_Click(object sender, EventArgs e)
{
try
{
dataGridViewTrial.Rows.RemoveAt(dataGridViewTrial.CurrentRow.Index);
sqlDataAdapter.Update(dataTable);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}
}


Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 11/18/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi Sudheep,

Please check this link http://khanrahim.wordpress.com/2010/04/10/insert-update-delete-with-datagridview-control-in-c-windows-application/ .

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 11/18/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

private void addUpadateButton_Click(object sender, EventArgs e)

{

try

{

sqlDataAdapter.Update(dataTable);// when i was trying to type the code as sqldataadapter.update-update will not coming in intellices

}

catch (Exception exceptionObj)

{

MessageBox.Show(exceptionObj.Message.ToString());// same problem

}

}

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 11/18/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

you can changes values in rows of grid. After change any value and click on the button, that change will reflect in grid and db. try this
also select any row and click delete button that row will disappear


Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: satender2008-12857 on: 12/5/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

I don't want to write the database code on the window page, i want write code in a class and accessing that class code on the page.

So, how is it possible ?

suppose if used there same code then it through error.




Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankit_parmar555 on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
can i get one sample windows application.(C# & ASP .net)
i m beginner for windows application so need to help from some ready made sample project.


Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kasani007 on: 7/27/2012 [Member] Starter | Points: 25

Up
0
Down
When i click on Add\update button, a dialog box showing syntax error INSERT INTO and when i click on Delete button it also shows a dialog "Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information"
why it is happening like please give me reply..

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response