|
|
|
ใช้งานกรอบตารางคล้ายๆ กับ DataGridView แต่ไม่ได้ใช้ฐานข้อมูล |
|
|
|
|
|
|
|
อันที่จริงคุณก็ใช้ DataGridView ก้ได้ครับ เพียงแต่สร้าง DataTable แล้วโยน Data Source แค่นั้นครับ
Code (C#)
DataTable dt = new DataTable();
DataRow dr;
//*** Column ***//
dt.Columns.Add("CustomerID");
dt.Columns.Add("Name");
dt.Columns.Add("Email");
dt.Columns.Add("CountryCode");
dt.Columns.Add("Budget");
dt.Columns.Add("Used");
//*** Rows ***//
dr = dt.NewRow();
dr["CustomerID"] = "C001";
dr["Name"] = "Win Weerachai";
dr["Email"] = "[email protected]";
dr["CountryCode"] = "TH";
dr["Budget"] = "1000000";
dr["Used"] = "600000";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["CustomerID"] = "C002";
dr["Name"] = "John Smith";
dr["Email"] = "[email protected]";
dr["CountryCode"] = "EN";
dr["Budget"] = "2000000";
dr["Used"] = "800000";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["CustomerID"] = "C003";
dr["Name"] = "Jame Born";
dr["Email"] = "[email protected]";
dr["CountryCode"] = "US";
dr["Budget"] = "3000000";
dr["Used"] = "600000";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["CustomerID"] = "C004";
dr["Name"] = "Chalee Angel";
dr["Email"] = "[email protected]";
dr["CountryCode"] = "US";
dr["Budget"] = "4000000";
dr["Used"] = "100000";
dt.Rows.Add(dr);
|
|
|
|
|
Date :
2011-11-08 09:06:14 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ช่วยสอนวิธีใช้งานอย่างละเอียดทีได้หรือเปล่าครับ
คือไม่เคยใช้งานdatagridviewเลย
|
|
|
|
|
Date :
2011-11-08 11:41:30 |
By :
-*- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอนนี้ทำได้แล้วครับขอบคุณมาก
แต่ว่าผมจะกำหนดขนาดความยาวของColumยังไงหรอครับ
|
|
|
|
|
Date :
2011-11-08 16:18:40 |
By :
สงัสย |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูนี่ครับ
Code (C# .NET)
private void PopulateData()
{
//*** BindData From Database **'
OleDbConnection objConn = default(OleDbConnection);
OleDbDataAdapter dtAdapter = default(OleDbDataAdapter);
DataTable dt = new DataTable();
string strConnString = null;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydatabase.mdb";
objConn = new OleDbConnection(strConnString);
objConn.Open();
string strSQL = null;
strSQL = "SELECT * FROM customer";
dtAdapter = new OleDbDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
dtAdapter = null;
objConn.Close();
objConn = null;
this.myDgv.DataSource = dt;
//******************************'
this.myDgv.Columns.Clear();
var _with1 = myDgv.ColumnHeadersDefaultCellStyle;
_with1.BackColor = Color.Navy;
_with1.ForeColor = Color.White;
_with1.Font = new Font(songsDataGridView.Font, FontStyle.Bold);
DataGridViewTextBoxColumn ColumnTextBox = default(DataGridViewTextBoxColumn);
//*** Column (Select) ***'
DataGridViewCheckBoxColumn ColumnCheckBox = new DataGridViewCheckBoxColumn();
ColumnCheckBox.Width = 30;
ColumnCheckBox.DataPropertyName = "Select";
this.myDgv.Columns.Add(ColumnCheckBox);
//*** Checkbox in Header ***'
CheckBox ckBox = new CheckBox();
Rectangle rect = myDgv.GetCellDisplayRectangle(0, -1, true);
ckBox.Size = new Size(18, 18);
rect.X = rect.Location.X + (rect.Width / 4);
ckBox.Location = rect.Location;
ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged);
this.myDgv.Controls.Add(ckBox);
myDgv.Columns(0).Frozen = false;
//*** Column (CustomerID) ***'
ColumnTextBox = new DataGridViewTextBoxColumn();
ColumnTextBox.DataPropertyName = "CustomerID";
ColumnTextBox.HeaderText = "CustomerID";
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
ColumnTextBox.Width = 100;
this.myDgv.Columns.Add(ColumnTextBox);
//*** Column (Name) ***'
ColumnTextBox = new DataGridViewTextBoxColumn();
ColumnTextBox.DataPropertyName = "Name";
ColumnTextBox.HeaderText = "Name";
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
ColumnTextBox.Width = 100;
this.myDgv.Columns.Add(ColumnTextBox);
//*** Column (Email) ***'
ColumnTextBox = new DataGridViewTextBoxColumn();
ColumnTextBox.DataPropertyName = "Email";
ColumnTextBox.HeaderText = "Email";
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
ColumnTextBox.Width = 100;
this.myDgv.Columns.Add(ColumnTextBox);
//*** Column (CountryCode) ***'
ColumnTextBox = new DataGridViewTextBoxColumn();
ColumnTextBox.DataPropertyName = "CountryCode";
ColumnTextBox.HeaderText = "CountryCode";
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
ColumnTextBox.Width = 100;
this.myDgv.Columns.Add(ColumnTextBox);
//*** Column (Budget) ***'
ColumnTextBox = new DataGridViewTextBoxColumn();
ColumnTextBox.DataPropertyName = "Budget";
ColumnTextBox.HeaderText = "Budget";
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
ColumnTextBox.Width = 100;
this.myDgv.Columns.Add(ColumnTextBox);
//*** Column (Used) ***'
ColumnTextBox = new DataGridViewTextBoxColumn();
ColumnTextBox.DataPropertyName = "Used";
ColumnTextBox.HeaderText = "Used";
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
ColumnTextBox.Width = 100;
this.myDgv.Columns.Add(ColumnTextBox);
}
|
|
|
|
|
Date :
2011-11-08 16:46:36 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ความยาวของ Column นั้นกำหนดได้ใน Property Width ของ Column เลยครับน่าจะเข้าตรง Edit Column หรือว่า Edit item นี่เเหละถ้าจำไม่ผิดนะ
|
|
|
|
|
Date :
2011-11-09 01:03:33 |
By :
Cyg |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|