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/
Best, Sudheep.
Login to post response