1, two property settings:
First: Set automatically create columns, default to True
DataGridView1. AutoGenerateColumns = True;
Although the default is true, but it is always good to write!!!
Second: Mouse click Edit, default double-click
Datagridview1.editmode = Datagridvieweditmode.editonenter;
2. Prevent cells from being edited:
DataGridViewCell: Restricting cells
DataGridViewColumn: Restricting Columns
DataGridViewRow: Restricting Rows
Datagridviewreadonly: Restricting the entire DataGridView control
The key code is as follows:
true; // The entire table is read -only datagridview1.columns[1true; Column read-only datagridview1.rows[0true; Line Read-only datagridview1[3,3true; Cell Read-only
Get Data
string strcolumn = Datagridview1.columns[e.columnindex]. HeaderText; // Get column headers string strrow = Datagridview1.rows[e.rowindex]. cells[0]. Value.tostring (); // gets the first value of the focus touch release string value = dataGridView1.CurrentCell.Value.ToString (); // gets the value of the currently clicked active cell
There is also a way to restrict editing, EditMode enumeration of editprogrammatically properties:
Datagridview1.editmode = datagridvieweditmode.editprogrammatically; // start editing only when you call the System.Windows.Forms.DataGridView.BeginEdit (System.Boolean) method
This property requires a Cellbeginedit event to be called, and the edit condition is written in the method.
Private void Datagridview1_cellbeginedit (object sender, DataGridViewCellCancelEventArgs e) { // datagridview1.editmode = datagridvieweditmode.editprogrammatically; // triggers this event when a cell is clicked, making conditional edits }
3, can be implemented through the Cellvaluechanged event to update the database data link
Private SqlConnection connection () { string"server = Xq-20160210kqle\\sa;uid = Sa;pwd = 123456;database = Jyxinxi "; New SqlConnection (strconn); return conn; }
DataGridView Displaying Data
Private voidGetdatagridview () {Try { stringStrda ="SELECT * from FilTer"; SqlConnection Conn=connection (); Conn. Open (); DataSet DS=NewDataSet (); SqlDataAdapter da=NewSqlDataAdapter (STRDA, Conn); Da. Fill (DS,"Show Data"); Conn. Close (); Datagridview1.autogeneratecolumns=true;//Automatically create columnsDatagridview1.editmode = Datagridvieweditmode.editonenter;//Click cell EditDatagridview1.datasource = ds. tables[0]; } Catch(Exception ee) {MessageBox.Show (EE. Message.tostring ()); } }
Update Database
Private voidDatagridview1_cellvaluechanged (Objectsender, DataGridViewCellEventArgs e) {SqlConnection conn=connection (); Try { stringStrcolumn = Datagridview1.columns[e.columnindex]. HeaderText;//Get column headers stringStrrow = Datagridview1.rows[e.rowindex]. cells[0]. Value.tostring ();//gets the first value of the focus touch release stringValue = DataGridView1.CurrentCell.Value.ToString ();//gets the value of the currently clicked active cell stringStrcomm ="Update FilTer Set"+ Strcolumn +"= '"+ Value +"' WHERE id ="+Strrow; //update FilTer Set column name = value WHERE id = 3Conn. Open (); SqlCommand Comm=NewSqlCommand (STRCOMM, conn); Comm. ExecuteNonQuery (); } Catch(Exception ee) {MessageBox.Show (EE. Message.tostring ()); } finally{Conn. Close (); } }
DataGridView modifying data and uploading to the database