全部代码如下:
namespace DataGridDoubleClick
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
gridMouseDownTime = DateTime.Now;
// Call SetUp to bind the controls.
SetUp();
}
private void SetUp()
{
// Create a DataSet with two tables and one relation.
MakeDataSet();
/* Bind the DataGrid to the DataSet. The dataMember
specifies that the Customers table should be displayed.*/
dataGrid1.SetDataBinding(myDataSet, "Customers");
//create and add a custom table style so we can
//easily get at the behavior of a cell...
AddCustomDataTableStyle();
}
private void MakeDataSet()
{
// Create a DataSet.
myDataSet = new DataSet("myDataSet");
// Create two DataTables.
DataTable tCust = new DataTable("Customers");
// Create two columns, and add them to the first table.
DataColumn cCustID = new DataColumn("custID");
DataColumn cCustName = new DataColumn("custName");
DataColumn cCurrent = new DataColumn("custCity");
tCust.Columns.Add(cCustID);
tCust.Columns.Add(cCustName);
tCust.Columns.Add(cCurrent);
// Add the tables to the DataSet.
myDataSet.Tables.Add(tCust);
/* Populates the tables. For each customer and order,
creates two DataRow variables. */
DataRow newRow1;
// Create three customers in the Customers Table.
for(int i = 1; i < 4; i++)
{
newRow1 = tCust.NewRow();
newRow1["custID"] = (100*i).ToString();
tCust.Rows.Add(newRow1);
}
// Give each customer a distinct name.
tCust.Rows[0]["custName"] = "John Summers";
tCust.Rows[1]["custName"] = "Phil Seagram";
tCust.Rows[2]["custName"] = "Sam Robinson";
这个是可以实现的,我给你一个我的代码:
private void SetUp()
{
string select="SELECT SUP_NO, COMPANY_NAME, ADDRESS FROM t_b_SUPPLIER WHERE STATUS!=9 ORDER BY SUP_NO ASC";
SqlDataAdapter da = new SqlDataAdapter(select,conn);
DataSet ds=new DataSet();
da.Fill(ds,"t_b_SUPPLIER");
dataGrid1.SetDataBinding(ds,"t_b_SUPPLIER");
}