-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataGridwithCellClickDb.cs
More file actions
57 lines (53 loc) · 1.82 KB
/
DataGridwithCellClickDb.cs
File metadata and controls
57 lines (53 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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;
using System.Data.SqlClient;
namespace WindowsFormsApp38
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-C25MSTP\\SQLEXPRESS; Initial Catalog=master; Integrated Security=true");
SqlCommand cmd = new SqlCommand("select empid as 'Employee ID',name as 'Employee Name',address as 'Address',salary as 'Salary' from emp", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
try
{
con.Open();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < dataGridView1.Rows.Count - 1)
{
Form3 dlg = new Form3(dataGridView1.SelectedRows[0].Cells["Employee ID"].Value.ToString(), dataGridView1.SelectedRows[0].Cells["Employee Name"].Value.ToString(), dataGridView1.SelectedRows[0].Cells["Address"].Value.ToString());
dlg.ShowDialog();
}
else
{
MessageBox.Show("select valid row", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}