|
 |
|
ขอโค้ด Update และ Delete หน่อยครับ ผมทำเว็บ Application โดยใช้ภาษา C# เขียน ต้องการทำหน้า Update |
|
 |
|
|
 |
 |
|
ไปดูวิธีการใช้ DataGridView ก่อนเลยครับ
แล้วค่อยมาถามว่าจะให้ update หรือ delete ยังไงอีกที
|
 |
 |
 |
 |
Date :
2010-01-25 14:03:08 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ครับ ผมใช้ Datagridview โชว์ข้อมูลโดยดึงมาจากฐานข้อมูล Ms SQL SERVER และสร้าง คอลัมน์ update และ delete ในDatagridview ได้แล้ว แล้วมันต้องใช้โค้ดอะไรบ้าง ช่วยบอกหน่อยครับไม่รู้จริงๆ
|
 |
 |
 |
 |
Date :
2010-01-25 21:07:28 |
By :
jorjarn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ฟอร์มของผมมี DataGridView อันเดียวครับ ชื่อว่า DataGridView1
ฐานข้อมูลที่ใช้เป็น ตารางชื่อเดือน มี 12 เดือน
ผมได้สร้าง Column ทั้งหมด 4 Column คือ ID (Read Only), MonthName (Allow Editing), Update Button และ Delete Button
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
DataGridView1.AllowUserToAddRows = false;
DataGridView1.AllowUserToResizeColumns = false;
DataGridView1.AllowUserToResizeRows = false;
DataGridView1.AutoGenerateColumns = false;
}
private void MainForm_Load(object sender, EventArgs e)
{
DataGridView1.DataSource = //Bind Data ของคุณที่นี่
DataGridViewTextBoxColumn IdColumn = new DataGridViewTextBoxColumn();
IdColumn.Name = "ID";
IdColumn.HeaderText = "ID";
IdColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
IdColumn.DataPropertyName = "MonthID";
IdColumn.ReadOnly = true;
DataGridViewTextBoxColumn MonthColumn = new DataGridViewTextBoxColumn();
MonthColumn.Name = "Month";
MonthColumn.HeaderText = "Month";
MonthColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
MonthColumn.DataPropertyName = "MonthName";
DataGridViewButtonColumn UpdateButtonColumn = new DataGridViewButtonColumn();
UpdateButtonColumn.Name = "Update";
UpdateButtonColumn.HeaderText = "Update";
UpdateButtonColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
UpdateButtonColumn.Text = "Update";
UpdateButtonColumn.UseColumnTextForButtonValue = true;
DataGridViewButtonColumn DeleteButtonColumn = new DataGridViewButtonColumn();
DeleteButtonColumn.Name = "Delete";
DeleteButtonColumn.HeaderText = "Delete";
DeleteButtonColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
DeleteButtonColumn.Text = "Delete";
DeleteButtonColumn.UseColumnTextForButtonValue = true;
DataGridView1.Columns.Add(IdColumn);
DataGridView1.Columns.Add(MonthColumn);
DataGridView1.Columns.Add(UpdateButtonColumn);
DataGridView1.Columns.Add(DeleteButtonColumn);
DataGridView1.CellClick += new DataGridViewCellEventHandler(DataGridView1_CellClick);
}
void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
switch (e.ColumnIndex)
{
case 2:
MessageBox.Show("Update " + DataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString(), "Msg Box");
//ใช้ Sql Command เพื่อ Update Data ที่นี่
break;
case 3:
MessageBox.Show("Delete " + DataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString(), "Msg Box");
//ใช้ Sql Command เพื่อ Delete Data ที่นี่
break;
}
DataGridView1.DataSource = //Bind Data ของคุณอีกทีที่นี่
}
}
}
|
 |
 |
 |
 |
Date :
2010-01-25 22:29:00 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยทำเป็นตัวอย่างให้ดูหน่อยได้มั้ยครับ ตรงที่เขียนว่า
-Bind Data
-Sql Command เพื่อ Update Data
- Sql Command เพื่อ Delete Data
คือว่าผมไม่รู้จริงๆนะครับ ช่วยบอกหน่อย
|
 |
 |
 |
 |
Date :
2010-01-28 20:02:01 |
By :
jorjarn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|