Public partial class Form4:form {SqlConnection conn = null;
SqlDataAdapter ADP = null;
DataSet ds = null; String constr = "Data source=.;
Initial Catalog=myschool;user Id=sa;pwd=sa ";
String sql = "SELECT * from grade";
Public Form4 () {InitializeComponent ();
conn = new SqlConnection (CONSTR);
ADP = new SqlDataAdapter (SQL, conn);
ds = new DataSet ("Grade");
#region------------data Query----------------private void Button1_Click (object sender, EventArgs e) {ds.
Clear (); Adp.
Fill (ds, "grade"); Datagridview1.datasource = ds.
Tables["Grade"]; #endregion #region------------data is saved and updated----------------private void button2_click (object sender,
EventArgs e) {SqlCommandBuilder builder = new SqlCommandBuilder (ADP); Adp. Update (ds, "Grade");
Updategridview (); #endregion #region----------Data deletion-----------------private void deletion Toolstripmenuitem_click (object
sender, EventArgs e) {SqlCommandBuilder builder = new SqlCommandBuilder (ADP);
int rowIndex = DataGridView1.CurrentRow.Index; Gets the first column content of the selected row MessageBox.Show (ds. Tables["Grade"]. Rows[rowindex][0].
ToString ()); ds. Tables["Grade"]. Rows[rowindex].
Delete (); Adp.
Update (ds, "grade");
Updategridview ();
#endregion #region Update after------------data operation----------------private void Updategridview () { Ds.
Clear (); Adp.
Fill (ds, "grade"); Datagridview1.datasource = ds.
Tables["Grade"]; } #endregion}
SqlCommandBuilder The data in the DataTable is updated back to the SQL database, the corresponding table in the SQL database must have a primary key; SqlDataAdapter does not automatically generate Transact-SQL statements required to reconcile changes between the DataSet and the associated SQL Server instance. However, if you set the SqlDataAdapter SelectCommand property, you can create a SqlCommandBuilder object to automatically generate Transact-SQL statements for a single table update. SqlCommandBuilder then generates any other Transact-SQL statements that are not set up.
Whenever the DataAdapter property is set, SqlCommandBuilder registers itself as a listener for the RowUpdating event. You can associate only one SqlDataAdapter with a SqlCommandBuilder object (or vice versa) at a time.
To generate an INSERT, UPDATE, or DELETE statement, SqlCommandBuilder automatically uses the SelectCommand property to retrieve the required metadata set. If you change SelectCommand after retrieving the metadata (for example, after the first update), you should call the RefreshSchema method to update the meta data.
SelectCommand must also return at least one primary key column or a unique column. If nothing is returned, a invalidoperation exception is generated and no command is built.
SqlCommandBuilder also uses the Connection, CommandTimeout, and Transaction properties referenced by SelectCommand. If one or more of these attributes are modified, or if the SelectCommand itself is replaced, the user should call RefreshSchema. Otherwise, the InsertCommand, UpdateCommand, and DeleteCommand properties all retain their previous values.
If you call Dispose, the SqlCommandBuilder associated with SqlDataAdapter is lifted, and the generated command is no longer used.
Gets the information in the selected row in the DataGridView
private void Tsmiadd_click (object sender, EventArgs e)
{
MessageBox.Show (this.dgvstudent.selectedrows[0]. Cells[0]. Value.tostring ());
}
You can also use lists to get information
private void Tmsideletecom_click (object sender, EventArgs e)
{
if (This.dgvStudent.SelectedRows.Count > 0)
{
MessageBox.Show (This.dgvstudent.selectedrows[0]. cells["studentno"]. Value);
}
}
The setting corresponding to the setting of the dgvstudent is consistent with the variable name in datapropertyname , as shown in the following figure: