Default ข้อมูลลง ComBoBox in DataGridView แบบช่องของ สถานะ
ผมก็ กำหนด แล้วนะครับ ตัวนี้อะครับ
Code (C#)
sql = "select * from receives_types";
sda = new SqlDataAdapter(sql, conn);
sda.Fill(dt);
i9.DisplayMember = "name";
i9.ValueMember = "id";
i9.DataSource = dt;
แต่มัน ไม่ได้อะครับ คือ ผมจะเอา receives_types ที่เป็น สถานะ อะครับ คือตอนแรกเก็บเป็น id 1 2 3 ไว้ แต่จะโชว์ เป็น name ยังไงครับ
ประวัติการแก้ไข 2015-11-02 15:10:37
Date :
2015-11-02 15:07:06
By :
phuriwat
มีคำถามมาให้คิดเล่นๆ
Code (C#)
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = item["receive_no"].ToString();
dataGridView1.Rows[n].Cells[1].Value = item["invoice_no"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["product_code"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["receive_id"].ToString();
dataGridView1.Rows[n].Cells[4].Value = item["product_name"].ToString();
dataGridView1.Rows[n].Cells[5].Value = item["product_unit_name"].ToString();
dataGridView1.Rows[n].Cells[6].Value = item["remark"].ToString();
dataGridView1.Rows[n].Cells[7].Value = item["quantity"].ToString();
dataGridView1.Rows[n].Cells[8].Value = item["receive_type_id"].ToString();
}
dt ที่ว่านี้มาจาก
sql = "select * from receives_types";
หรือ
sql = "select * FROM receive_details JOIN receives ON receives.id=receive_details.receive_id join stock_incomings on stock_incomings.receive_detail_id=receive_details.id";
Date :
2015-11-02 15:21:56
By :
lamaka.tor
Code (C#)
private void load_default()
{
if (conn.State == ConnectionState.Closed)
conn.Open();
string sql = "select * FROM receive_details JOIN receives ON receives.id=receive_details.receive_id join stock_incomings on stock_incomings.receive_detail_id=receive_details.id";
SqlDataAdapter sda = new SqlDataAdapter(sql
, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.Rows.Clear();
sql = "select * from receives_types";
sda = new SqlDataAdapter(sql, conn);
sda.Fill(dt);//// dt ถูก set ค่าใหม่
i9.DisplayMember = "name";
i9.ValueMember = "id";
i9.DataSource = dt; // dt น่าจะมาจาก sql = "select * from receives_types";
foreach (DataRow item in dt.Rows) // dt น่าจะมาจาก sql = "select * from receives_types";
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = item["receive_no"].ToString();
dataGridView1.Rows[n].Cells[1].Value = item["invoice_no"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["product_code"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["receive_id"].ToString();
dataGridView1.Rows[n].Cells[4].Value = item["product_name"].ToString();
dataGridView1.Rows[n].Cells[5].Value = item["product_unit_name"].ToString();
dataGridView1.Rows[n].Cells[6].Value = item["remark"].ToString();
dataGridView1.Rows[n].Cells[7].Value = item["quantity"].ToString();
dataGridView1.Rows[n].Cells[8].Value = item["receive_type_id"].ToString();
}
}
มันเป็นแบบนั้นป่าวครับ
รึผมเดาผิด 555
Date :
2015-11-02 15:37:58
By :
lamaka.tor
DataTable แบบบ้านๆมาฝากใช้ง่ายไม่งงดี
Code (C#)
private DataTable Table(String SQL)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(SQL, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
Return dt;
}
ใช้งาน
Code (C#)
dataGridView1.DataSource = Table("select * FROM receive_details JOIN receives ON receives.id=receive_details.receive_id join stock_incomings on stock_incomings.receive_detail_id=receive_details.id");
i9.DataSource = Table("select * from receives_types");
Date :
2015-11-02 15:44:20
By :
lamaka.tor
คือตอนแรก อะครับ
Code (C#)
"select * FROM receive_details JOIN receives ON receives.id=receive_details.receive_id join stock_incomings on stock_incomings.receive_detail_id=receive_details.id"
3ตารางนี้จะมี receive_type_id อยู่ครับ ซึ่งเป็น status ครับ ถ้าเอาลงมาเก็บไว่ใน columntext มันจะเป็นตัวเลขอะครับ
แต่ถ้าจะเอาไว้ใน datagridcombobox แบบ มาถึงก็ โหลดลงมาเลยอะครับ เช่นถ้า id เป็น 1 ก็จะโชว์ปกติ 2 ก็จะล็อก ไรงี้อะครับ
ส่วนอันนี้
Code (C#)
"select * from receives_types"
คือผมจะไห้มันไป เอาว่า id 1 คืออะไร 2 คืออะไรอ่ะครับ
Date :
2015-11-02 15:55:11
By :
phuriwat
ประเด็นคือ
foreach (DataRow item in dt.Rows) // dt น่าจะมาจาก sql = "select * from receives_types";
ไม่ได้มาจาก
"select * FROM receive_details JOIN receives ON receives.id=receive_details.receive_id join stock_incomings on stock_incomings.receive_detail_id=receive_details.id"
ครับ
sql = "select * from receives_types";
sda = new SqlDataAdapter(sql, conn);
sda.Fill(dt);//// dt ถูก set ค่าใหม่
ลองเขียนใหม่ดูๆๆ
Date :
2015-11-02 16:01:29
By :
lamaka.tor
https://www.thaicreate.com/community/csharp-datasource-wizard-datagridviewcomboboxcolumn.html
ง่ายดีครับ
แต่ถ้าอยากเขียนเป็นโค้ดก็
Code (C#)
this.dataGridViewTextBoxColumn1.DataPropertyName = "PostCodeID";
this.dataGridViewTextBoxColumn1.HeaderText = "PostCodeID";
this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
//
// dataGridViewTextBoxColumn2
//
this.dataGridViewTextBoxColumn2.DataPropertyName = "PostCode";
this.dataGridViewTextBoxColumn2.HeaderText = "PostCode";
this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
//
// dataGridViewTextBoxColumn3
//
this.dataGridViewTextBoxColumn3.DataPropertyName = "Pnv";
this.dataGridViewTextBoxColumn3.DataSource = this.sett_Post_ProvinceBindingSource;
this.dataGridViewTextBoxColumn3.DisplayMember = "Province";
this.dataGridViewTextBoxColumn3.HeaderText = "Pnv";
this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3";
this.dataGridViewTextBoxColumn3.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.dataGridViewTextBoxColumn3.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.dataGridViewTextBoxColumn3.ValueMember = "pvnID";
//
// dataGridViewTextBoxColumn4
//
this.dataGridViewTextBoxColumn4.DataPropertyName = "Amp";
this.dataGridViewTextBoxColumn4.HeaderText = "Amp";
this.dataGridViewTextBoxColumn4.Name = "dataGridViewTextBoxColumn4";
//
// dataGridViewTextBoxColumn5
//
this.dataGridViewTextBoxColumn5.DataPropertyName = "PosOff";
this.dataGridViewTextBoxColumn5.HeaderText = "PosOff";
this.dataGridViewTextBoxColumn5.Name = "dataGridViewTextBoxColumn5";
//
// dataGridViewTextBoxColumn6
//
this.dataGridViewTextBoxColumn6.DataPropertyName = "Note";
this.dataGridViewTextBoxColumn6.HeaderText = "Note";
this.dataGridViewTextBoxColumn6.Name = "dataGridViewTextBoxColumn6";
this.sett_Post_PostCodeDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.dataGridViewTextBoxColumn1,
this.dataGridViewTextBoxColumn2,
this.dataGridViewTextBoxColumn3,
this.dataGridViewTextBoxColumn4,
this.dataGridViewTextBoxColumn5,
this.dataGridViewTextBoxColumn6});
this.sett_Post_PostCodeDataGridView.DataSource = this.sett_Post_PostCodeBindingSource;
Date :
2015-11-02 17:13:09
By :
lamaka.tor
น่าจะประมาณนี้ครับ
Code (C#)
private DataTable GetTable(String SQL)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(SQL, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
Return dt;
}
Code (C#)
// Set Column
DataGridViewTextBoxColumn c1 = new DataGridViewTextBoxColumn() { DataPropertyName = "receive_no", HeaderText = "receive_no" };
DataGridViewTextBoxColumn c2 = new DataGridViewTextBoxColumn() { DataPropertyName = "invoice_no", HeaderText = "invoice_no" };
DataGridViewTextBoxColumn c3 = new DataGridViewTextBoxColumn() { DataPropertyName = "product_code", HeaderText = "product_code" };
DataGridViewTextBoxColumn c4 = new DataGridViewTextBoxColumn() { DataPropertyName = "receive_id", HeaderText = "receive_id" };
DataGridViewTextBoxColumn c5 = new DataGridViewTextBoxColumn() { DataPropertyName = "product_name", HeaderText = "product_name" };
DataGridViewTextBoxColumn c6 = new DataGridViewTextBoxColumn() { DataPropertyName = "product_unit_name", HeaderText = "product_unit_name" };
DataGridViewTextBoxColumn c7 = new DataGridViewTextBoxColumn() { DataPropertyName = "remark", HeaderText = "remark" };
DataGridViewTextBoxColumn c8 = new DataGridViewTextBoxColumn() { DataPropertyName = "quantity", HeaderText = "quantity" };
DataGridViewComboBoxColumn c9 = new DataGridViewComboBoxColumn() { DataPropertyName = "receive_type_id", HeaderText = "receive_type_id", DataSource = GetTable(select * from receives_types), DisplayMember = "name", ValueMember = "id" };
// Add Column
dataGridView1.Columns.AddRange(c1,c2,c3,c4,c5,c6,c7,c8,c9);
// Set DataSource
dataGridView1.DataSource =GetTable (select * FROM receive_details JOIN receives ON receives.id=receive_details.receive_id join stock_incomings on stock_incomings.receive_detail_id=receive_details.id);
บ้านไก็น่าจะประมาณนี้ครับ
Date :
2015-11-02 17:25:41
By :
lamaka.tor
Load balance : Server 04