|
 |
|
C# DGV ส่งค่าไปแก้ไข้ RadioButton ยังไง/ บันทึกแล้ว ให้ DGV เคลียร์ |
|
 |
|
|
 |
 |
|

ดึงค่าข้อมุลไป โชว์ที่ DGV แล้ว กดที่ DGV ให้ ส่งข้อมูลไป แก้ไข ที่ TextBox /RadioButton ยังไงหรอคับ
1.ติดในส่วนของ RadioButton
2.เมื่อกด แก้ไข ให้ DGV ทำการ เคลียข้อมูล เริ่มใหม่
ขอบคุณล่วงหน้าคับ ผมมือใหม่ กำลังศึกษาคับ
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.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
string gender;
string status;
private void button3_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
{
Config config = new Config();
string constring = config.ConnString;
conn.ConnectionString = constring;
conn.Open();
string sql = "SELECT * FROM tbNCD WHERE HN_N ='"+textBox3.Text+"' ";
SqlCommand com = new SqlCommand(sql, conn);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(dt);
dataGridView1.Rows.Clear();
foreach (DataRow row in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = row["HN_N"].ToString();
dataGridView1.Rows[n].Cells[1].Value = row["Name_N"].ToString();
dataGridView1.Rows[n].Cells[2].Value = row["Gender_N"].ToString();
dataGridView1.Rows[n].Cells[3].Value = row["Bag_N"].ToString();
}
textBox3.Clear();
textBox3.Focus();
}
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
gender = "ชาย";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
gender = "หญิง";
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
status = "ไม่มี";
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
status = "มี";
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form1 frm1 = new Form1();
frm1.Show();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox3.Select();
}
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex == -1) return;
textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
gender = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
status = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
if ((textBox1.Text == "") || (textBox2.Text == "") || (radioButton1.Checked == false) && (radioButton2.Checked == false) ||
(radioButton3.Checked == false) && (radioButton4.Checked == false))
{
MessageBox.Show("กรุณากรอกข้อมูลให้ครบ", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
using (SqlConnection conn = new SqlConnection())
{
Config config = new Config();
string constring = config.ConnString;
conn.ConnectionString = constring;
conn.Open();
string sql = "UPDATE tbNCD SET HN_N = '" + textBox1.Text + "' ,Name_N = '" + textBox2.Text + "' ,Gender_N = '" +
gender + "' ,Bag_N = '" + status + "' WHERE HN_N ='" + textBox1.Text + "' ";
SqlCommand com = new SqlCommand(sql, conn);
com.ExecuteNonQuery();
conn.Close();
MessageBox.Show("แก้ไขสำเร็จ");
textBox1.Clear();
textBox2.Clear();
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
textBox1.Focus();
conn.Close();
}
}
}
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
}
}
}
Tag : Ms SQL Server 2012, C#

|
|
 |
 |
 |
 |
Date :
2018-01-03 16:31:00 |
By :
destiny9999 |
View :
868 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ if เอาครับ เช่น
Code (C#)
if(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString() == "1")
{
radioButton1.Checked = true;
}
else
{
radioButton1.Checked = false;
}
|
 |
 |
 |
 |
Date :
2018-01-05 12:56:29 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|