Updating a sql checkbox

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 6
Reputation: markyjj is an unknown quantity at this point 
Solved Threads: 0
markyjj markyjj is offline Offline
Newbie Poster

Updating a sql checkbox

 
0
  #1
Aug 13th, 2009
Can someone please help me with the following problem. I currently have a datagrid linked to an sql database and in the grid I have a ckeckbox column. However, when I check the box in the current cell the checked box disappears when I click on another cell and only reappears only when I go back to the cell. I was wandering how I get the checked box to stay visible even when I click on another cell.


I would be grateful for any advice, many thanks.







  1. Can someone please help me with the following problem. I currently have a datagrid linked to an sql database and in the grid I have a ckeckbox column. However, when I check the box in the current cell the checked box disappears when I click on another cell and only reappears only when I go back to the cell. I was wandering how I get the checked box to stay visible even when I click on another cell.
  2.  
  3. I would be grateful for any advice, many thanks.
  4.  
  5.  
  6. //Define controls must be added to data grid.
  7. private ComboBox cboControl = new ComboBox();
  8. private ComboBox cboControl2 = new ComboBox();
  9.  
  10.  
  11. //Control definishion to add to DataGrid
  12. DataGridTableStyle dataGridStyle = new DataGridTableStyle();
  13.  
  14. //Create the TextBox columns for the datagrid columns
  15. DataGridTextBoxColumn aCol1 = new DataGridTextBoxColumn();
  16. DataGridTextBoxColumn aCol2 = new DataGridTextBoxColumn();
  17. DataGridTextBoxColumn aCol3 = new DataGridTextBoxColumn();
  18. DataGridTextBoxColumn aCol4 = new DataGridTextBoxColumn();
  19. DataGridTextBoxColumn aCol5 = new DataGridTextBoxColumn();
  20. DataGridTextBoxColumn aCol6 = new DataGridTextBoxColumn();
  21. CheckBox chk1 = new CheckBox();
  22. DataView Dv;
  23. public string Movstring;
  24. public string Combostr;
  25. public int CurRow = -1;
  26. public int CurCol = -1;
  27. public string CellValue;
  28. public bool strValue = false;
  29. private System.Windows.Forms.Button button11;
  30. private System.Windows.Forms.Button button12;
  31. private System.Windows.Forms.Button Findbtn;
  32. public System.Windows.Forms.Button UpdateSavebtn;
  33. public bool imageswitch;
  34. public void Imagescrn()
  35. {
  36. this.BackgroundImage = Image.FromFile("C:\\Documents and Settings\\Mark\\My Documents\\My Pictures\\house_pg2.jpg");
  37. }
  38. public void ImageMain()
  39. {
  40. this.BackgroundImage = Image.FromFile("C:\\Documents and Settings\\Mark\\My Documents\\My Pictures\\bg_page.jpg");
  41. }
  42.  
  43.  
  44.  
  45. static void Main()
  46. {
  47. Application.Run(new Form1());
  48. }
  49.  
  50.  
  51.  
  52.  
  53. public void Form1_Load(object sender, System.EventArgs e)
  54. {
  55. //Add a new table style to datagrid.
  56. DesignTableStyle();
  57. dataGrid1.RowHeadersVisible = true;
  58. dataGrid1.DataSource = Movset1;
  59. MovAdapt.Fill(Movset1, "MovTable1");
  60. dataGrid1.DataSource = Movset1;
  61. label1.Show();
  62. Show();
  63. dataGrid1.Show();
  64.  
  65. }
  66.  
  67.  
  68. //Add a new table style to datagrid.
  69. private void DesignTableStyle()
  70. {
  71.  
  72. cboControl.Cursor = Cursors.Arrow;
  73. cboControl2.Cursor = Cursors.Arrow;
  74.  
  75. dataGridStyle.MappingName= "MovTable1";
  76. dataGridStyle.PreferredRowHeight = 24;
  77.  
  78. //
  79.  
  80. // Create GridColumnStyle objects for the grid columns
  81. //
  82.  
  83. aCol1.HeaderText = "Item No";
  84. aCol1.MappingName = "Movie_Index";
  85. aCol1.Width = 50;
  86.  
  87.  
  88. dataGridStyle.GridColumnStyles.Add(aCol1);
  89.  
  90.  
  91. dataGrid1.TableStyles.Add(dataGridStyle);
  92.  
  93.  
  94. aCol2.HeaderText = "Title";
  95. aCol2.MappingName = "Title";
  96. aCol2.Width = 100;
  97. dataGridStyle.GridColumnStyles.Add(aCol2);
  98.  
  99.  
  100. dataGridComboBox.HeaderText = "Category";
  101. dataGridComboBox.MappingName = "Category";
  102. dataGridComboBox.Width = 130;
  103. dataGridStyle.GridColumnStyles.Add(dataGridComboBo x);
  104.  
  105. dataGridComboBox2.HeaderText = "Features";
  106. dataGridComboBox2.MappingName = "Features";
  107. dataGridComboBox2.Width = 130;
  108. dataGridStyle.GridColumnStyles.Add(dataGridComboBo x2);
  109.  
  110. aCol6.HeaderText = "Boxset";
  111. aCol6.MappingName = "Boxset";
  112. aCol6.Width = 100;
  113. dataGridStyle.GridColumnStyles.Add(aCol6);
  114. aCol6.TextBox.Controls.Add(chk1);
  115.  
  116.  
  117. }
  118.  
  119.  
  120. private void label1_Click(object sender, System.EventArgs e)
  121. {
  122.  
  123. }
  124.  
  125.  
  126.  
  127. private void button2_Click(object sender, System.EventArgs e)
  128. {
  129. this.Close();
  130. }
  131.  
  132.  
  133. private void button4_Click(object sender, System.EventArgs e)
  134. {
  135. dataGrid1.Hide();
  136. label1.Hide();
  137. Imagescrn();
  138. Dv = new DataView(Movset1.Tables[0]);
  139. dataGrid1.DataSource = Dv;
  140. Dv.RowFilter = "Category = 'HORROR'";
  141. }
  142.  
  143. private void button6_Click(object sender, System.EventArgs e)
  144. {
  145. Dv = new DataView(Movset1.Tables[0]);
  146. dataGrid1.DataSource = Dv;
  147. Dv.RowFilter = "Category = 'Action/Thriller'";
  148. }
  149.  
  150. private void button8_Click(object sender, System.EventArgs e)
  151. {
  152. label1.Hide();
  153. this.BackgroundImage = Image.FromFile("C:\\Documents and Settings\\Mark\\My Documents\\My Pictures\\drama_pg.jpg");
  154. Dv = new DataView(Movset1.Tables[0]);
  155. dataGrid1.DataSource = Dv;
  156. Dv.RowFilter = "Category = 'Drama'";
  157. }
  158.  
  159. private void button5_Click(object sender, System.EventArgs e)
  160. {
  161. Dv = new DataView(Movset1.Tables[0]);
  162. dataGrid1.DataSource = Dv;
  163. Dv.RowFilter = "Category = 'Family/Entertainment'";
  164. }
  165.  
  166. private void button7_Click(object sender, System.EventArgs e)
  167. {
  168. label1.Hide();
  169. this.BackgroundImage = Image.FromFile("C:\\Documents and Settings\\Mark\\My Documents\\My Pictures\\scifi_pg.jpg");
  170. Dv = new DataView(Movset1.Tables[0]);
  171. dataGrid1.DataSource = Dv;
  172. Dv.RowFilter = "Category = 'Sci-Fi/Fantasy'";
  173. }
  174.  
  175. private void button9_Click(object sender, System.EventArgs e)
  176. {
  177. Dv = new DataView(Movset1.Tables[0]);
  178. dataGrid1.DataSource = Dv;
  179. Dv.RowFilter = "Category = 'TV/Series'";
  180. }
  181.  
  182. private void button10_Click(object sender, System.EventArgs e)
  183. {
  184. ImageMain();
  185. dataGrid1.RowHeadersVisible = true;
  186. dataGrid1.DataSource = Movset1;
  187. label1.Show();
  188. Show();
  189. }
  190.  
  191.  
  192. private void textBox1_TextChanged(object sender, System.EventArgs e)
  193. {
  194. Movstring = textBox1.ToString();
  195. }
  196.  
  197. private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
  198. {
  199. int Currncol = 0;
  200. Currncol = dataGrid1.CurrentCell.ColumnNumber;
  201. CurCol = dataGrid1.CurrentCell.ColumnNumber;
  202. CurRow = dataGrid1.CurrentCell.RowNumber;
  203. dataGrid1.RowHeadersVisible = false;
  204.  
  205. Currncol = dataGrid1.CurrentCell.ColumnNumber;
  206.  
  207. if(Currncol == 2) {
  208.  
  209.  
  210. Combostr = this.cboControl.SelectedText.ToString();
  211. cboControl.SelectedIndexChanged+=new EventHandler(cboSelectedIndexChanged);
  212.  
  213. }
  214.  
  215. if(Currncol == 3)
  216. {
  217.  
  218.  
  219. Combostr = this.cboControl2.SelectedText.ToString();
  220. cboControl2.SelectedIndexChanged+=new EventHandler(cbo2SelectedIndexChanged);
  221.  
  222. }
  223.  
  224. if(Currncol == 4)
  225. {
  226. MessageBox.Show("Checked col has been clicked");
  227. chk1.CheckedChanged +=new EventHandler(chk1_CheckedChanged);
  228.  
  229.  
  230. }
  231.  
  232.  
  233. Currncol = 0;
  234.  
  235. }
  236.  
  237.  
  238.  
  239. private void Form1_Enter(object sender, System.EventArgs e)
  240. {
  241. dataGrid1.RowHeadersVisible = true;
  242.  
  243. }
  244.  
  245. private void button12_Click(object sender, System.EventArgs e)
  246. {
  247.  
  248. dataGrid1.Show();
  249.  
  250. }
  251.  
  252. private void Findbtn_Click(object sender, System.EventArgs e)
  253. {
  254. Dv = new DataView(Movset1.Tables[0]);
  255. dataGrid1.DataSource = Dv;
  256. Dv.RowFilter = "Title ='"+textBox1.Text+"'";
  257. if(textBox1.Text == "")
  258. {
  259. MessageBox.Show("Search failed: Movie Title has not been entered!! ");
  260. return;
  261. }
  262.  
  263. if(Dv.Count < 1)
  264. {
  265. MessageBox.Show("Movie does not exist in MovieBase, please try again");
  266. }
  267.  
  268. }
  269.  
  270. private void UpdateSavebtn_Click_1(object sender, System.EventArgs e)
  271. {
  272. Mcon.Open();
  273. CurCol = dataGrid1.CurrentCell.ColumnNumber;
  274. CurRow = dataGrid1.CurrentCell.RowNumber;
  275. CellValue = dataGrid1[CurRow, CurCol].ToString();
  276. MessageBox.Show(CellValue.ToString());
  277. if(CellValue == "")
  278. {
  279.  
  280. MessageBox.Show("No Fields can be Blank - update failed");
  281. Movset1.RejectChanges();
  282. dataGrid1.Show();
  283.  
  284.  
  285. CurRow = -1;
  286. CurCol = -1;
  287. Mcon.Close();
  288. return;
  289.  
  290. }
  291. CellValue = dataGrid1[CurRow, CurCol].ToString();
  292. CurRow = -1;
  293. CurCol = -1;
  294.  
  295. MovAdapt.UpdateCommand = cb.GetUpdateCommand();
  296. MovAdapt.Update(Movset1.Tables[0]);
  297. Movset1.AcceptChanges();
  298.  
  299. MessageBox.Show("MovieBase has been updated");
  300.  
  301. Mcon.Close();
  302.  
  303.  
  304. dataGrid1.RowHeadersVisible = true;
  305. }
  306.  
  307. private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  308. {
  309.  
  310. hitTestGrid = dataGrid1.HitTest(e.X, e.Y);
  311.  
  312. //Add combobox control to datagrid.
  313. dataGridComboBox.TextBox.Controls.Add( cboControl );
  314. cboControl.Items.AddRange(new string[8]{"","Horror","Action/Thriller","TV/Series","Sci-fi/Fantasy","Family","Drama","Comedy"});
  315.  
  316. dataGridComboBox2.TextBox.Controls.Add( cboControl2 );
  317. cboControl2.Items.AddRange(new string[4]{"","Standard","Extended Edtn","Directors Cut"});
  318.  
  319.  
  320.  
  321. }
  322.  
  323. //Initialize Conrols for adding in DataGrid cells.
  324. private void InitializeControls()
  325. {
  326. //combobox property
  327. cboControl.Cursor = Cursors.Hand;
  328. cboControl.DropDownStyle = ComboBoxStyle.DropDownList;
  329. //combobox events.
  330.  
  331. }
  332. private void cboSelectedIndexChanged(object sender, System.EventArgs e)
  333. {
  334.  
  335. dataGrid1[CurRow, CurCol] = cboControl.SelectedItem.ToString();
  336. CurRow = 0;
  337. CurCol = 0;
  338.  
  339. }
  340.  
  341. private void cbo2SelectedIndexChanged(object sender, System.EventArgs e)
  342. {
  343. dataGrid1[CurRow, CurCol] = cboControl2.SelectedItem.ToString();
  344. CurRow = 0;
  345. CurCol = 0;
  346.  
  347.  
  348. }
  349.  
  350. private void chk1_CheckedChanged(object sender, EventArgs e)
  351. {
  352. if(chk1.Checked)
  353. {
  354. CurCol = 0;
  355. CurCol = 0;
  356. chk1.Enabled = true;
  357.  
  358.  
  359.  
  360. }
  361.  
  362. }
  363. }
  364. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC