Guest User

Untitled

a guest
Jul 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. private void Form5_Load(object sender, EventArgs e)
  2. {
  3. DataTable dt = new DataTable();
  4. dt.Columns.Add("name");
  5. for (int j = 0; j < 10; j++)
  6. {
  7. dt.Rows.Add("");
  8. }
  9. this.dataGridView1.DataSource = dt;
  10. this.dataGridView1.Columns[0].Width = 200;
  11.  
  12. /*
  13. * First method : Convert to an existed cell type such ComboBox cell,etc
  14. */
  15.  
  16. DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();
  17. ComboBoxCell.Items.AddRange(new string[] { "aaa","bbb","ccc" });
  18. this.dataGridView1[0, 0] = ComboBoxCell;
  19. this.dataGridView1[0, 0].Value = "bbb";
  20.  
  21. DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();
  22. this.dataGridView1[0, 1] = TextBoxCell;
  23. this.dataGridView1[0, 1].Value = "some text";
  24.  
  25. DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell();
  26. CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
  27. this.dataGridView1[0, 2] = CheckBoxCell;
  28. this.dataGridView1[0, 2].Value = true;
  29.  
  30. /*
  31. * Second method : Add control to the host in the cell
  32. */
  33. DateTimePicker dtp = new DateTimePicker();
  34. dtp.Value = DateTime.Now.AddDays(-10);
  35. //add DateTimePicker into the control collection of the DataGridView
  36. this.dataGridView1.Controls.Add(dtp);
  37. //set its location and size to fit the cell
  38. dtp.Location = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Location;
  39. dtp.Size = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Size;
  40. }
Add Comment
Please, Sign In to add comment