|
 |
|
Edit Data ใน DataGridView แล้วบันทึกลงฐานข้อมูลไม่ได้ |
|
 |
|
|
 |
 |
|
อันนี้ code Showdata
Code (C#)
private void Show_Data(string _txtIdDurRecei)
{
try
{
string connectionString = dc.Getdb.ToString(); //เรียกใช้ dbconnec
conn = new MySqlConnection(connectionString);
conn.Open();
string sql = "SELECT dur_id, dur_name, durGroup_name, durtype_name, dur_detail, dur_amount, a.dur_price, unit_name, dur_date FROM tbl_durables";
dt = new DataTable();
da = new MySqlDataAdapter(sql, conn);
da.Fill(dt);
conn.Close();
dgDisplay.DataSource = dt;
dgDisplay.Columns.Clear();
DataGridViewTextBoxColumn column;
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "dur_id";
column.HeaderText = "รหัสครุภัณฑ์";
column.Width = 90;
column.ReadOnly = true;
this.dgDisplay.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "dur_name";
column.HeaderText = "ชื่อครุภัณฑ์";
column.Width = 160;
column.ReadOnly = false;
this.dgDisplay.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "durGroup_name";
column.HeaderText = "กลุ่มครุภัณฑ์";
column.Width = 200;
column.ReadOnly = true;
this.dgDisplay.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "durtype_name";
column.HeaderText = "ประเภทครุภัณฑ์";
column.Width = 130;
column.ReadOnly = true;
this.dgDisplay.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "dur_detail";
column.HeaderText = "รายละเอียด";
column.Width = 155;
column.ReadOnly = false;
this.dgDisplay.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "dur_amount";
column.HeaderText = "จำนวน";
column.Width = 50;
column.ReadOnly = true;
column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
this.dgDisplay.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "dur_price";
column.HeaderText = "ราคา";
column.Width = 80;
column.ReadOnly = false;
column.DefaultCellStyle.Format = "#,##0.0";
column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
this.dgDisplay.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "unit_name";
column.HeaderText = "หน่วย";
column.Width = 70;
column.ReadOnly = false;
this.dgDisplay.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "dur_date";
column.HeaderText = "วันที่";
column.Width = 85;
column.ReadOnly = true;
this.dgDisplay.Columns.Add(column);
DataGridViewCheckBoxColumn ColumnCheckBox = new DataGridViewCheckBoxColumn();
ColumnCheckBox.Width = 45;
ColumnCheckBox.DataPropertyName = "Select";
ColumnCheckBox.HeaderText = "เลือก";
this.dgDisplay.Columns.Add(ColumnCheckBox);
dt = null;
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
พอแสดงโชว์ใน datagridview ผมแก้ไขข้อมูลบางส่วน เช่น column ชื่อครุภัณฑ์ จากนั้นกดปุ่ม update ข้อมูลที่แก้ไขไม่อัพเดทในฐานข้อมูล ผมทำผิดขั้นตอนไหนครับ รบกวนคำแนะนำหน่อยครับ ขอบคุณครับ
code ปุ่มอัพเดท
Code (C#)
try
{
da = new MySqlDataAdapter();
dt = new DataTable();
da.Update(dt);
MessageBox.Show("Changes Done");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Tag : .NET, C#

|
|
 |
 |
 |
 |
Date :
2014-11-28 11:43:18 |
By :
Takashi_7 |
View :
876 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองศึกษาบทความนี้ดูครับ

.NET Windows Form Application เขียนโปรแกรมบน Windows Form
|
 |
 |
 |
 |
Date :
2014-11-28 16:27:07 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|