943,883 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 800
  • C# RSS
Apr 1st, 2009
0

Griview + Checkboxes

Expand Post »
I bound gridview with databse..property Checkbox -true .
I have three columns roll,name ,roll.

I want that on button_click, roll nos are displayed whose checkboxes are checked in gridview--
SOURCE TAB
C# Syntax (Toggle Plain Text)
  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
  2.  
  3. <Columns>
  4.  
  5. <asp:TemplateField >
  6.  
  7. <ItemTemplate >
  8.  
  9. <asp:CheckBox ID ="chk" runat ="server" />
  10.  
  11. </ItemTemplate>
  12.  
  13. </asp:TemplateField>
  14.  
  15. <asp:BoundField HeaderText="Roll" DataField ="roll" />
  16.  
  17. <asp:BoundField HeaderText="Name" DataField ="name"/>
  18.  
  19. <asp:BoundField HeaderText="Marks" DataField ="marks"/>
  20.  
  21. </Columns>
  22.  
  23. </asp:GridView>
  24.  
  25. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
  26.  
  27. <Columns>
  28.  
  29. <asp:TemplateField >
  30.  
  31. <ItemTemplate >
  32.  
  33. <asp:CheckBox ID ="chk" runat ="server" />
  34.  
  35. </ItemTemplate>
  36.  
  37. </asp:TemplateField>
  38.  
  39. <asp:BoundField HeaderText="Roll" DataField ="roll" />
  40.  
  41. <asp:BoundField HeaderText="Name" DataField ="name"/>
  42.  
  43. <asp:BoundField HeaderText="Marks" DataField ="marks"/>
  44.  
  45. </Columns>
  46.  
  47. </asp:GridView>


C# Syntax (Toggle Plain Text)
  1. public partial class Default3 : System.Web.UI.Page
  2. {
  3. string query;
  4. SqlConnection conn = new SqlConnection("Data Source=SQLEXPRESS;Initial catalog=mansi;Integrated Security=true");
  5. SqlCommand cmd;
  6. protected void Page_Load(object sender, EventArgs e)
  7. {
  8. query = "Select * from Information";
  9. conn.Open();
  10. cmd = new SqlCommand(query, conn);
  11. SqlDataAdapter da = new SqlDataAdapter(cmd);
  12. DataSet ds=new DataSet ();
  13. da.Fill (ds );
  14. GridView1.DataSource = ds;
  15. GridView1.DataBind();
  16. conn.Close();
  17. }
  18.  
  19.  
  20. protected void Button1_Click(object sender, EventArgs e)
  21. {
  22. bool bchecked;
  23.  
  24. for (int i = 0; i < this.GridView1.Rows.Count; i++)
  25. {
  26. bchecked = ((CheckBox)GridView1.Rows[i].FindControl("chk")).Checked;
  27.  
  28. }
  29. }
  30. }



Suppose i have three rows in Gridview & I Uncheck first & third checkboxes,& check the second checkbox,
But everytime in varable bchecked false is coming...y so???Plz help me out..
Last edited by mansi sharma; Apr 1st, 2009 at 2:34 am.
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
mansi sharma is offline Offline
75 posts
since Apr 2008
Apr 1st, 2009
0

Re: Griview + Checkboxes

Hi
The code mentioned in PageLoad.. gets repeated for every postback,
try this way...

Quote ...
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback)
{
query = "Select * from Information";
conn.Open();
cmd = new SqlCommand(query, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds=new DataSet ();
da.Fill (ds );
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
}
Reputation Points: 10
Solved Threads: 6
Light Poster
vizy is offline Offline
36 posts
since Dec 2007
Apr 1st, 2009
0

Re: Griview + Checkboxes

Thx Very much,It worked...........
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
mansi sharma is offline Offline
75 posts
since Apr 2008
Apr 1st, 2009
0

Re: Griview + Checkboxes

hi frnd, Now i got the checboxes status,But now I want to get the cell value of second column whose corresponding checkboxes are checked.

On Net,I found but its not working....Help me out...

C# Syntax (Toggle Plain Text)
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. string sName;
  4. bool bchecked;
  5.  
  6. for (int i = 0; i < this.GridView1.Rows.Count; i++)
  7. {
  8. bchecked = ((CheckBox)GridView1.Rows[i].FindControl("chk")).Checked;
  9. if (bchecked == true)
  10. {
  11. Label lblName = (Label)GridView1.FindControl("Name") ;
  12. sName = lblName.ToString ;
  13.  
  14. }
  15.  
  16.  
  17. }
  18.  
  19.  
  20. }


SOURCE TAB
C# Syntax (Toggle Plain Text)
  1. <asp:BoundField HeaderText="Roll" DataField ="roll" />
  2. <asp:BoundField HeaderText="Name" DataField ="name"/>
  3. <asp:BoundField HeaderText="Marks" DataField ="marks"/>
Last edited by mansi sharma; Apr 1st, 2009 at 4:04 pm.
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
mansi sharma is offline Offline
75 posts
since Apr 2008
Apr 2nd, 2009
0

Re: Griview + Checkboxes

Hi,

For the Control Values.. U need to convert your Bound Controls to TemplateFields. chk this

Quote ...
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label runat="server" ID="Name" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
then you can FindControl these values.. in your .cs file
Reputation Points: 10
Solved Threads: 6
Light Poster
vizy is offline Offline
36 posts
since Dec 2007
Apr 3rd, 2009
0

Re: Griview + Checkboxes

thx very much.......
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
mansi sharma is offline Offline
75 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Reading in a .txt file, splitting it and then outputting the data to a listbox
Next Thread in C# Forum Timeline: NEED HELP WITH Xpath Quries





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC