|
 |
|
การผูกข้อมูล datagrid กับ combobox ตอนคลิก datagrid แก้ไขข้อมูลให้มันแสดงประเภทที่เลือกไว้ |
|
 |
|
|
 |
 |
|
การแสดงข้อมูลประเภทสินค้าตอนคลิก datagrid ไม่ต้องการแสดง รหัสประเภทสินค้าใน datagrid
ตรงบรรดที่ระบุนะครับเป็นการส่งค่ามาจาก datagrid เนื่องใน datagrid มีการ
select * from product,type where product.type_id=type.type_id
ซึ่งความเป็นจริงแล้วเราจะไม่แสดงรหัสประเภทสินค้าใน datagrid
ซึ่งจะเขียน sql ใหม่เป็น select pro_id,proname,pro_price,pro_cost,type_name from product,type where product.type_id=type.type_id
ใน datagrid ก็จะไม่แสดงรหัสประเภทสินค้าแล้ว
ทำให้ไม่สามารถอ่านค่า type_id จาก datagrid มากำหนด cboType.SelectedValue ได้
ถ้าเวลาคลิกอยากได้ค่า type_id มากำหนด cboType.SelectedValue โดยที่ ไม่แสดงรหัสประเภทสินค้าใน datagrid จะใช้วิธีไหนดีครับ
Code (C#)
private void grdProduct_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow grdrow = this.grdProduct.Rows[e.RowIndex];
pro_id = grdrow.Cells["pro_id"].Value.ToString();
txtProid.Text = grdrow.Cells["pro_id"].Value.ToString();
txtProname.Text = grdrow.Cells["pro_name"].Value.ToString();
rtxtDetail.Text = grdrow.Cells["pro_detail"].Value.ToString();
txtCost.Text = grdrow.Cells["pro_cost"].Value.ToString();
txtPrice.Text = grdrow.Cells["pro_price"].Value.ToString();
try
{
DBConnect db = new DBConnect();
string sql2 = " select * from type ";
MySqlCommand SelectCommand2 = new MySqlCommand(sql2, db.connection);
MySqlDataAdapter dataadapter = new MySqlDataAdapter(SelectCommand2);
DataSet dataset = new DataSet();
dataadapter.Fill(dataset, "type");
if (dataset.Tables["type"].Rows.Count > 0)
{
cboType.DisplayMember = "type_name";
cboType.ValueMember = "type_id";
cboType.DataSource = dataset.Tables["type"];
//ตรงบรรทัดนี้ครับ
cboType.SelectedValue = grdrow.Cells["type_id"].Value.ToString();
}
db.CloseConnection();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Tag : .NET, MySQL, C#, VS 2010 (.NET 4.x)

|
ประวัติการแก้ไข 2014-04-29 17:27:57 2014-04-29 17:28:31
|
 |
 |
 |
 |
Date :
2014-04-29 17:25:43 |
By :
narubet |
View :
854 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ ผมกำลังทำแบบเอา id สินค้าไป query มาแล้วได้ id ต่างๆมา
เอาค่าไปกำหนด
Code (C#)
DataGridViewRow grdrow = this.grdCustomer.Rows[e.RowIndex];
cusid = grdrow.Cells["cus_id"].Value.ToString();
try
{
DBConnect db = new DBConnect();
string sql = " select * from customer where cus_id=?cusid";
MySqlCommand SelectCommand = new MySqlCommand(sql, db.connection);
db.OpenConnection();
SelectCommand.Parameters.Add(new MySqlParameter("?cusid", cusid));
MySqlDataReader reader = SelectCommand.ExecuteReader();
while (reader.Read())
{
province = reader["cus_province"].ToString();
amphur = reader["cus_amphur"].ToString();
district = reader["cus_district"].ToString();
}
reader.Close();
db.CloseConnection();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "pro");
}
cboCusprovince.SelectedValue = province;
cboCusamphur.SelectedValue = amphur;
cboCusdistrict.SelectedValue = district;
|
 |
 |
 |
 |
Date :
2014-04-30 09:44:04 |
By :
narubet |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|