DataGridView1
How do I use Data Grid View1
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: FormLop.cs
Copy
30 private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
31 {
32 dong = e.RowIndex;
33 txtMaLop.Text=dgvLop.Rows[dong].Cells[0].Value.ToString();
34 txtTenLop.Text = dgvLop.Rows[dong].Cells[1].Value.ToString();
35 cmbMaKhoiLop.SelectedValue = dgvLop.Rows[dong].Cells[2].Value.ToString();
36 cmbMaGV.SelectedValue = dgvLop.Rows[dong].Cells[3].Value.ToString();
37 }
File name: Form1.cs
Copy
228 public void Button5_Click(System.Object sender, System.EventArgs e)
229 {
230 newgrade = gradeclass.DataModule.GetLoad();
231 DataGridView1.DataSource = newgrade;
232 DataGridView1.DataMember = "grade1";
233
234 }
File name: Form1.cs
Copy
347 public void Button6_Click(System.Object sender, System.EventArgs e)
348 {
349
350 TextBox1.Text = DataGridView1.CurrentRow.Cells[1].Value.ToString();
351 TextBox2.Text = DataGridView1.CurrentRow.Cells[2].Value.ToString();
352 TextBox3.Text = DataGridView1.CurrentRow.Cells[3].Value.ToString();
353 TextBox4.Text = DataGridView1.CurrentRow.Cells[4].Value.ToString();
354 TextBox5.Text = DataGridView1.CurrentRow.Cells[5].Value.ToString();
355 TextBox6.Text = DataGridView1.CurrentRow.Cells[6].Value.ToString();
356 TextBox7.Text = DataGridView1.CurrentRow.Cells[7].Value.ToString();
357 TextBox8.Text = DataGridView1.CurrentRow.Cells[8].Value.ToString();
358 TextBox9.Text = DataGridView1.CurrentRow.Cells[9].Value.ToString();
359 TextBox10.Text = DataGridView1.CurrentRow.Cells[10].Value.ToString();
360 TextBox11.Text = DataGridView1.CurrentRow.Cells[11].Value.ToString();
361 TextBox12.Text = DataGridView1.CurrentRow.Cells[12].Value.ToString();
362 TextBox13.Text = DataGridView1.CurrentRow.Cells[13].Value.ToString();
363 TextBox14.Text = DataGridView1.CurrentRow.Cells[14].Value.ToString();
364 TextBox15.Text = DataGridView1.CurrentRow.Cells[15].Value.ToString();
365 TextBox16.Text = DataGridView1.CurrentRow.Cells[16].Value.ToString();
366 TextBox17.Text = DataGridView1.CurrentRow.Cells[17].Value.ToString();
367 TextBox18.Text = DataGridView1.CurrentRow.Cells[18].Value.ToString();
368 TextBox19.Text = DataGridView1.CurrentRow.Cells[19].Value.ToString();
369 STUDID.Text = DataGridView1.CurrentRow.Cells[0].Value.ToString();
370
371 }
File name: Form1.cs
Copy
397 public void DataGridView1_CellDoubleClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
398 {
399 TextBox1.Text = DataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
400 TextBox2.Text = DataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
401 TextBox3.Text = DataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
402 TextBox4.Text = DataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
403 TextBox5.Text = DataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
404 TextBox6.Text = DataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
405 TextBox7.Text = DataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
406 TextBox8.Text = DataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString();
407 TextBox9.Text = DataGridView1.Rows[e.RowIndex].Cells[9].Value.ToString();
408 TextBox10.Text = DataGridView1.Rows[e.RowIndex].Cells[10].Value.ToString();
409 TextBox11.Text = DataGridView1.Rows[e.RowIndex].Cells[11].Value.ToString();
410 TextBox12.Text = DataGridView1.Rows[e.RowIndex].Cells[12].Value.ToString();
411 TextBox13.Text = DataGridView1.Rows[e.RowIndex].Cells[13].Value.ToString();
412 TextBox14.Text = DataGridView1.Rows[e.RowIndex].Cells[14].Value.ToString();
413 TextBox15.Text = DataGridView1.Rows[e.RowIndex].Cells[15].Value.ToString();
414 TextBox16.Text = DataGridView1.Rows[e.RowIndex].Cells[16].Value.ToString();
415 TextBox17.Text = DataGridView1.Rows[e.RowIndex].Cells[17].Value.ToString();
416 TextBox18.Text = DataGridView1.Rows[e.RowIndex].Cells[18].Value.ToString();
417 TextBox19.Text = DataGridView1.Rows[e.RowIndex].Cells[19].Value.ToString();
418 STUDID.Text = DataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
419
420
421 }
File name: gradeclass.cs
Copy
68 public void RefreshDGV()
69 {
70
71 System.Data.OleDb.OleDbConnection conn = GetConnection();
72 string sql = default(string);
73 sql = "SELECT STUDENTID,data1 AS Firstname, data2 AS Lastname," + "data3 AS 20, data4 AS EG, data5 AS [1st-10%], data6 AS 50pt," + "data7 AS EG1, data8 AS [2nd-10%], data9 AS 50pts, data10 AS EG2," + "data11 AS [20%], data12 AS 100pt, data13 AS EG3, data14 AS [30%]," + "data15 AS 100, data16 AS EG4, data17 AS [2nd-30%], data18 AS PrelimGrade," + "data19 AS Remarks FROM Table1 ORDER BY ID";
74
75 System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sql, conn);
76 DataTable dt = new DataTable("grade1");
77 da.Fill(dt);
78 Form1.Default.DataGridView1.DataSource = dt;
79 }
File name: gradeclass.cs
Copy
225 public DataSet deldata(string sortfield)
226 {
227
228 System.Data.OleDb.OleDbConnection conn = GetConnection();
229 DataSet ds = new DataSet();
230
231 try
232 {
233
234 int inc;
235 int MaxRows;
236
237 //Dim sql As String = "select * from table1"
238 //Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, conn)
239
240 try
241 {
242
243 string sqldelete = default(string);
244 sqldelete = "DELETE * FROM table1 WHERE STUDENTID=\'" + Form1.Default.STUDID.Text + "\'";
245 System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sqldelete, conn);
246
247 // Gets the records from the table and fills our adapter with those.
248 DataTable dt = new DataTable("grade1");
249 da.Fill(dt);
250 MessageBox.Show("Data has been deleted");
251 clearall();
252
253 Form1.Default.DataGridView1.DataSource = dt;
254
255 RefreshDGV();
256
257 }
258 finally
259 {
260 //da.Dispose()
261 }
262
263 return ds;
264 }
265 finally
266 {
267 conn.Close();
268 conn.Dispose();
269 }
270 }
File name: DeleteMembers.cs
Copy
20 private void button1_Click(object sender, EventArgs e)
21 {
22 try
23 {
24 SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\Sem.4\C# Projects\GymManagementSystem in C#\GymManagementSystem\Gym.mdf;Integrated Security=True;User Instance=True");
25 con.Open();
26
27 string str = "DELETE FROM members WHERE m_id = '" + textBox1.Text + "'";
28
29 SqlCommand cmd = new SqlCommand(str, con);
30 cmd.ExecuteNonQuery();
31 con.Close();
32 MessageBox.Show(" Members Record Deleted Successfully");
33
34 using (SqlConnection newcon = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\Sem.4\C# Projects\GymManagementSystem in C#\GymManagementSystem\Gym.mdf;Integrated Security=True;User Instance=True"))
35 {
36 string str1 = "SELECT * from members";
37 SqlCommand cmd1 = new SqlCommand(str1, newcon);
38 SqlDataAdapter da = new SqlDataAdapter(cmd1);
39 DataTable dt = new DataTable();
40 da.Fill(dt);
41 dataGridView1.DataSource = new BindingSource(dt, null);
42
43 }
44 }
45
46 catch (SqlException ex)
47 {
48 MessageBox.Show(ex.Message);
49 MessageBox.Show("Please Enter Employee Id..");
50 }
51 }
File name: DeleteMembers.cs
Copy
53 private void DeleteMembers_Load(object sender, EventArgs e)
54 {
55 // TODO: This line of code loads data into the 'gymDataSet1.members' table. You can move, or remove it, as needed.
56 this.membersTableAdapter.Fill(this.gymDataSet1.members);
57 using (SqlConnection newcon = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\Sem.4\C# Projects\GymManagementSystem in C#\GymManagementSystem\Gym.mdf;Integrated Security=True;User Instance=True"))
58 {
59 string str1 = "SELECT * from members";
60 SqlCommand cmd1 = new SqlCommand(str1, newcon);
61 SqlDataAdapter da = new SqlDataAdapter(cmd1);
62 DataTable dt = new DataTable();
63 da.Fill(dt);
64 dataGridView1.DataSource = new BindingSource(dt, null);
65
66 }
67 }
File name: SearchMembers.cs
Copy
20 private void button1_Click(object sender, EventArgs e)
21 {
22 using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\Sem.4\C# Projects\GymManagementSystem in C#\GymManagementSystem\Gym.mdf;Integrated Security=True;User Instance=True"))
23 {
24
25 string str = "SELECT * FROM members WHERE m_id = '" + textBox1.Text + "'";
26 SqlCommand cmd = new SqlCommand(str, con);
27 SqlDataAdapter da = new SqlDataAdapter(cmd);
28 DataTable dt = new DataTable();
29 da.Fill(dt);
30
31 dataGridView1.DataSource = new BindingSource(dt, null);
32 }
33
34 textBox1.Text = "";
35 }
File name: SearchMembers.cs
Copy
37 private void SearchMembers_Load(object sender, EventArgs e)
38 {
39 // TODO: This line of code loads data into the 'gymDataSet.members' table. You can move, or remove it, as needed.
40 this.membersTableAdapter.Fill(this.gymDataSet.members);
41 using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\Sem.4\C# Projects\GymManagementSystem in C#\GymManagementSystem\Gym.mdf;Integrated Security=True;User Instance=True"))
42 {
43
44 string str = "SELECT * FROM members";
45 SqlCommand cmd = new SqlCommand(str, con);
46 SqlDataAdapter da = new SqlDataAdapter(cmd);
47 DataTable dt = new DataTable();
48 da.Fill(dt);
49
50 dataGridView1.DataSource = new BindingSource(dt, null);
51 }
52 }
DataGridView1 132 lượt xem
Gõ tìm kiếm nhanh...