Griview + Checkboxes

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 75
Reputation: mansi sharma is an unknown quantity at this point 
Solved Threads: 0
mansi sharma mansi sharma is offline Offline
Junior Poster in Training

Griview + Checkboxes

 
0
  #1
Apr 1st, 2009
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
  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>


  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: vizy is an unknown quantity at this point 
Solved Threads: 6
vizy's Avatar
vizy vizy is offline Offline
Light Poster

Re: Griview + Checkboxes

 
0
  #2
Apr 1st, 2009
Hi
The code mentioned in PageLoad.. gets repeated for every postback,
try this way...

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();
}
}
Njoy koding... >>>
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 75
Reputation: mansi sharma is an unknown quantity at this point 
Solved Threads: 0
mansi sharma mansi sharma is offline Offline
Junior Poster in Training

Re: Griview + Checkboxes

 
0
  #3
Apr 1st, 2009
Thx Very much,It worked...........
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 75
Reputation: mansi sharma is an unknown quantity at this point 
Solved Threads: 0
mansi sharma mansi sharma is offline Offline
Junior Poster in Training

Re: Griview + Checkboxes

 
0
  #4
Apr 1st, 2009
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...

  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: vizy is an unknown quantity at this point 
Solved Threads: 6
vizy's Avatar
vizy vizy is offline Offline
Light Poster

Re: Griview + Checkboxes

 
0
  #5
Apr 2nd, 2009
Hi,

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

<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
Njoy koding... >>>
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 75
Reputation: mansi sharma is an unknown quantity at this point 
Solved Threads: 0
mansi sharma mansi sharma is offline Offline
Junior Poster in Training

Re: Griview + Checkboxes

 
0
  #6
Apr 3rd, 2009
thx very much.......
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 591 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC