|
|
|
ขอโค้ด การสร้าง data grid หน่อยคะ ลองทำแล้วแต่ใช้ DataBind();ไม่ได้ |
|
|
|
|
|
|
|
ดูตัวอย่างตามที่ผมให้ครับมันใช้แค่
Code (VB.NET)
Me.dgName.DataSource = dt
|
|
|
|
|
Date :
2010-10-27 18:32:34 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ของนู๋ใช้ C# อ่าคะ
private void frmMain_Load(object sender, EventArgs e)
{
BindDataGrid();
}
private void BindDataGrid()
{
SqlConnection myConnection = default(SqlConnection);
DataTable dt = new DataTable();
SqlDataAdapter Adapter = default(SqlDataAdapter);
//myConnection = new SqlConnection("Data Source =" + (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "\\AppDatabase1.sdf;"));
myConnection = new SqlConnection("Data Source =C:\\WindowsFormsApplication\\WindowsFormsApplication\\Project.sdf;");
myConnection.Open();
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "SELECT [ID] FROM [User]";
myCommand.CommandType = CommandType.Text;
Adapter = new SqlDataAdapter(myCommand);
Adapter.Fill(dt);
myConnection.Close();
this.dgName.DataSource = dt;
this.dgName.Columns.Clear();
DataGridViewTextBoxColumn column;
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "ID";
column.HeaderText = "รหัสประจำตัว";
column.Width = 50;
this.dgName.Columns.Add(column);
dt = null;
}
void BindData()
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataAdapter dtAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
String strConnString, strSQL;
strConnString = "Server=localhost;Uid=sa;PASSWORD=1234;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
strSQL = "SELECT * FROM User WHERE (ID like '%" + txtSearch + "%') ";
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
//*** BindData to GridView ***
dgName.DataSource = ds;
dgName.DataBind(); มันติดตรงนี่คะ
dtAdapter = null;
objConn.Close();
objConn = null;
}
private void btnSearch_Click(object sender, EventArgs e)
{
BindData();
}
|
|
|
|
|
Date :
2010-10-27 18:46:08 |
By :
rimupare |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|