DataGridView modifying data and uploading to the database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.