Reference Code 1:
Simulate the data yourself and assign values to the DataGridView, respectively.
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Collections;namespacewindowsformsapplication1{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } DataTable Tbldatas=NewDataTable ("Datas"); Private voidForm1_Load (Objectsender, EventArgs e) {DataColumn DC=NULL; DC= TBLDATAS.COLUMNS.ADD ("ID", Type.GetType ("System.Int32")); dc. AutoIncrement=true;//automatically adddc. AutoIncrementSeed =1;//starting at 1dc. AutoIncrementStep =1;//Step size is 1dc. AllowDBNull =false;//DC= TBLDATAS.COLUMNS.ADD ("Product", Type.GetType ("System.String")); DC= TBLDATAS.COLUMNS.ADD ("Version", Type.GetType ("System.String")); DC= TBLDATAS.COLUMNS.ADD ("Description", Type.GetType ("System.String")); DataRow NewRow; for(inti =0; I < -; i++) {NewRow=Tbldatas.newrow (); newrow["Product"] ="Western Tour"; newrow["Version"] ="2.0"; newrow["Description"] ="I love it ."; TBLDATAS.ROWS.ADD (NewRow); } NewRow=Tbldatas.newrow (); newrow["Product"] ="Dream West Tour"; newrow["Version"] ="3.0"; newrow["Description"] ="more childish than a liar"; TBLDATAS.ROWS.ADD (NewRow); Datagridview1.datasource=Tbldatas; //DataGridView1 } Private voidDatagridview1_cellvaluechanged (Objectsender, DataGridViewCellEventArgs e) { //DataGridView Change DataSet Data following changes;Tbldatas.rows[e.rowindex][e.columnindex]=Datagridview1.rows[e.rowindex]. Cells[e.columnindex]. Value.tostring (); } Private voidButton1_Click (Objectsender, EventArgs e) { //This is the dataset data changes DataGridView following changes;//This is based on the fact that only the data of one cell in the dataset can be modified. tbldatas.rows[5][1] ="5555555555"; } }}
Reference Code 2
Manually assign values separately.
DataSet ds_sp =NewDataSet ();D atatable dt_sp=NewDataTable ();if(string. IsNullOrEmpty (strfilter)) {strfilter="1=1";} DT_SP= Dbhelper.getsqltasklist (tablename,"*", sortexpr, This. Sgvtask.pagesize, PageIndex,0, strfilter, Sortdirect);//Use the page turn function in SQLDataTable Dttask=dt_sp; for(inti =0; i < DtTask.Rows.Count; i++) {DataRow Dr=dt. NewRow ();d r["YMD"] = dttask.rows[i]["YMD"]. ToString ();d r["Sdshan"] = dttask.rows[i]["Sdshan"]. ToString ();d r["Abalph"] = dttask.rows[i]["Abalph"]. ToString ();d r["Xsje"] = dttask.rows[i]["Xsje"]. ToString ();d r["Fentan_month"] = dttask.rows[i]["Fentan_month"]. ToString ();d r["Cont_kebian"] = dttask.rows[i]["Cont_kebian"]. ToString ();d r["Contract_num"] = dttask.rows[i]["Contract_num"]. ToString ();d r["Fentan_mode"] = dttask.rows[i]["Fentan_mode"]. ToString ();d r["Fentan_bl_je"] = dttask.rows[i]["Fentan_bl_je"]. ToString ();d r["CJ"] = dttask.rows[i]["CJ"]. ToString ();d r["Fixsmordernum"] = dttask.rows[i]["Fixsmordernum"]. ToString ();d r["FTFs"] = dttask.rows[i]["FTFs"]. ToString ();d r["Ftdai"] = dttask.rows[i]["Ftdai"]. ToString ();d r["daikm"] = dttask.rows[i]["daikm"]. ToString ();d r["Ftjie"] = dttask.rows[i]["Ftjie"]. ToString ();d r["jiekm"] = dttask.rows[i]["jiekm"]. ToString ();d r["MEMO"] = dttask.rows[i]["MEMO"]. ToString ();d T. Rows.Add (DR);} This. Sgvtask.datasource =DT; This. Sgvtask.databind ();
Reference Mode 3 Mapping method:
First define some columns in DataGridView, set the datapropertyname of these columns to the column names of the related DataTable in the returned dataset of the database that the query gets.
Specific property settings can refer to this blog post: C # DataGridView's Column Object properties discussion (not yet continued).
Reference
C # DataGridView How to bind to a dataset to achieve the following goals
C # How to make DataGridView1 display data, the DataGridView1 column corresponds to the column one by one in the database table
C # DataGridView is bound to a dataset, and the column corresponds to column one by one in the database table