C#

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

Join Date: Oct 2009
Posts: 1
Reputation: sawiyan is an unknown quantity at this point 
Solved Threads: 0
sawiyan sawiyan is offline Offline
Newbie Poster

C#

 
0
  #1
27 Days Ago
Hay
i am new to the developnment currently i am working on datagrid
i just want to know how to select a row on a datagrid using check boxes
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 117
Reputation: vinnijain is an unknown quantity at this point 
Solved Threads: 10
vinnijain vinnijain is offline Offline
Junior Poster
 
-1
  #2
27 Days Ago
You can try following code for ASP:

  1. //HTML part of DemoPage1.aspx
  2. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoPage1.aspx.cs" Inherits="DemoPage1" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head id="Head1" runat="server">
  8. <title>GridView Sample By Ramesh S</title>
  9.  
  10. <script type="text/javascript">
  11. function selectUnselectCheckboxes(chkBoxHeaderId, chkBoxIdInGrid, gridViewId) {
  12. var gridViewElem = document.getElementById(gridViewId);
  13.  
  14. //get the no of rows<tr> generated by the gridview
  15. var gridRowsCount = gridViewElem.rows.length;
  16. //the controls in the gridview will have the following suffix in their ids 'GridView1_ctl0n'
  17.  
  18. //the first element starts with 2 e.g. GridView1_ctl02_chkEmployee(first element)... GridView1_ctl10_chkEmployee(9th element)
  19.  
  20. for (i = 2; i <= gridRowsCount; i++) {
  21. var ctlIndex = eval(i);
  22. if (ctlIndex < 10)
  23. ctlIndex = '0' + i;
  24. var chkBoxIdFull = gridViewElem.id + '_ctl' + ctlIndex + '_' + chkBoxIdInGrid;
  25. var chkBoxElem = document.getElementById(chkBoxIdFull);
  26. if (chkBoxElem != null) {
  27. //if the header checkbox is checked, then check the checkboxes in the grid
  28. if (document.getElementById(chkBoxHeaderId).checked)
  29. chkBoxElem.checked = true;
  30. else
  31. chkBoxElem.checked = false;
  32. }
  33. }
  34.  
  35. }
  36. </script>
  37. </head>
  38. <body>
  39. <form id="form1" runat="server">
  40. <div>
  41. <table width="100%">
  42. <tr>
  43. <td>
  44.  
  45. <asp:GridView ID="GridView1" runat="server" Width="500px" BackColor="White" BorderColor="#3366CC"
  46. BorderStyle="None" BorderWidth="1px" CellPadding="4" OnRowDataBound="GridView1_RowDataBound"
  47. AutoGenerateColumns="False">
  48. <RowStyle BackColor="White" ForeColor="#003399" />
  49. <Columns>
  50. <asp:TemplateField>
  51. <HeaderTemplate>
  52. <asp:CheckBox ID="chkSelectAll" ToolTip="Select/Unselect all employees" onclick="selectUnselectCheckboxes(this.id, 'chkEmployee', 'GridView1');"
  53. runat="server" />
  54. </HeaderTemplate>
  55. <ItemTemplate>
  56. <asp:CheckBox ID="chkEmployee" runat="server" Checked='<%# Bind("Is_Eligible") %>' />
  57. </ItemTemplate>
  58. <HeaderStyle HorizontalAlign="Center" />
  59. <ItemStyle HorizontalAlign="Center" Width="50px" />
  60. </asp:TemplateField>
  61. <asp:BoundField DataField="EmpName" HeaderText="Name">
  62. <HeaderStyle HorizontalAlign="Left" />
  63. </asp:BoundField>
  64. <asp:BoundField DataField="EmpCode" HeaderText="Code">
  65. <HeaderStyle HorizontalAlign="Left" />
  66. </asp:BoundField>
  67. <asp:TemplateField HeaderText="Dept">
  68. <ItemTemplate>
  69. <asp:DropDownList ID="ddlDept" DataTextField="Dname" DataValueField="Deptno" DataSource="<%#GetAllDept()%>"
  70. runat="server">
  71. </asp:DropDownList>
  72. </ItemTemplate>
  73. </asp:TemplateField>
  74. </Columns>
  75. <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
  76. <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
  77. <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
  78. <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
  79. </asp:GridView>
  80.  
  81. </td>
  82. </tr>
  83. </table>
  84. </div>
  85. </form>
  86. </body>
  87. </html>
  88. //Code behind part of DemoPage1.aspx.cs
  89.  
  90. using System;
  91. using System.Web.UI.WebControls;
  92. using System.Data;
  93. public partial class DemoPage1 : System.Web.UI.Page
  94. {
  95. protected void Page_Load(object sender, EventArgs e)
  96. {
  97. if (!IsPostBack)
  98. {
  99. //Bind gridview with employee details
  100. BindGridView();
  101. }
  102. }
  103. //This method binds DataTable to GridView
  104. //I have created a DataTable with sample data here
  105. //You can fetch records from database, store it in a DataTable and bind it
  106. private void BindGridView()
  107. {
  108. DataTable dtEmp = new DataTable();
  109. dtEmp.Columns.Add("Is_Eligible", System.Type.GetType("System.Boolean"));
  110. dtEmp.Columns.Add("EmpName", System.Type.GetType("System.String"));
  111. dtEmp.Columns.Add("EmpCode", System.Type.GetType("System.Int16"));
  112. dtEmp.Columns.Add("Deptno", System.Type.GetType("System.Int16"));
  113. dtEmp.Rows.Add(new object[] { true, "Kannan", 1001, 10 });
  114. dtEmp.Rows.Add(new object[] { true, "Arun", 1001, 10 });
  115. dtEmp.Rows.Add(new object[] { false, "Jackson", 1001, 30 });
  116. dtEmp.Rows.Add(new object[] { true, "Raja", 1001, 20 });
  117. dtEmp.Rows.Add(new object[] { false, "John", 1001, 30 });
  118. GridView1.DataSource = dtEmp;
  119. GridView1.DataBind();
  120. }
  121. //This method returns a DataTable having department details to be
  122.  
  123. //bind with DropDownList for each record in the GridView
  124. public DataTable GetAllDept()
  125. {
  126. DataTable dtDept = new DataTable();
  127. dtDept.Columns.Add("Deptno", System.Type.GetType("System.Int16"));
  128. dtDept.Columns.Add("Dname", System.Type.GetType("System.String"));
  129. dtDept.Rows.Add(new object[] { 10, "Accounts" });
  130. dtDept.Rows.Add(new object[] { 20, "Sales" });
  131. dtDept.Rows.Add(new object[] { 30, "Operations" });
  132. return dtDept;
  133. }
  134. //This event is handled to bind DropDownList in the GridView
  135. //with department details
  136. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  137. {
  138. if (e.Row.RowType == DataControlRowType.DataRow)
  139. {
  140.  
  141. DropDownList ddlDeptNo = (DropDownList)e.Row.FindControl("ddlDept");
  142. string deptNo = ((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[3].ToString();
  143. if (ddlDeptNo != null)
  144. {
  145. ddlDeptNo.SelectedValue = deptNo;
  146. }
  147. }
  148. }
  149. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training
 
0
  #3
27 Days Ago
Hello, just to make sure .. you have WinForms Project or Web-vased?
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 132
Reputation: babbu has a little shameless behaviour in the past 
Solved Threads: 13
babbu babbu is offline Offline
Junior Poster

if ur looking for winforms code

 
0
  #4
26 Days Ago
first add a check box column to the datagridview.
to do this click on the datagridview control on ur form.
click on the right arrow on the top of ur datagrid.
click edit columns
add a column and set its type to check box column
  1. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  2. {
  3.  
  4. if (e.ColumnIndex == //column index of check box column)
  5. {
  6. if (Convert.ToBoolean(dataGridView1.CurrentCell.Value) == true)
  7. {
  8. dataGridView1.CurrentCell.Value = false;
  9. }
  10.  
  11. else
  12. {
  13. dataGridView1.CurrentCell.Value = true;
  14.  
  15. }
  16. }
  17. }
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC