| | |
Griview + Checkboxes
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 75
Reputation:
Solved Threads: 0
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
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..
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)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField > <ItemTemplate > <asp:CheckBox ID ="chk" runat ="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Roll" DataField ="roll" /> <asp:BoundField HeaderText="Name" DataField ="name"/> <asp:BoundField HeaderText="Marks" DataField ="marks"/> </Columns> </asp:GridView> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField > <ItemTemplate > <asp:CheckBox ID ="chk" runat ="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Roll" DataField ="roll" /> <asp:BoundField HeaderText="Name" DataField ="name"/> <asp:BoundField HeaderText="Marks" DataField ="marks"/> </Columns> </asp:GridView>
C# Syntax (Toggle Plain Text)
public partial class Default3 : System.Web.UI.Page { string query; SqlConnection conn = new SqlConnection("Data Source=SQLEXPRESS;Initial catalog=mansi;Integrated Security=true"); SqlCommand cmd; protected void Page_Load(object sender, EventArgs e) { 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(); } protected void Button1_Click(object sender, EventArgs e) { bool bchecked; for (int i = 0; i < this.GridView1.Rows.Count; i++) { bchecked = ((CheckBox)GridView1.Rows[i].FindControl("chk")).Checked; } } }
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.
Hi
The code mentioned in PageLoad.. gets repeated for every postback,
try this way...
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... >>>
•
•
Join Date: Apr 2008
Posts: 75
Reputation:
Solved Threads: 0
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...
SOURCE TAB
On Net,I found but its not working....Help me out...
C# Syntax (Toggle Plain Text)
protected void Button1_Click(object sender, EventArgs e) { string sName; bool bchecked; for (int i = 0; i < this.GridView1.Rows.Count; i++) { bchecked = ((CheckBox)GridView1.Rows[i].FindControl("chk")).Checked; if (bchecked == true) { Label lblName = (Label)GridView1.FindControl("Name") ; sName = lblName.ToString ; } } }
SOURCE TAB
C# Syntax (Toggle Plain Text)
<asp:BoundField HeaderText="Roll" DataField ="roll" /> <asp:BoundField HeaderText="Name" DataField ="name"/> <asp:BoundField HeaderText="Marks" DataField ="marks"/>
Last edited by mansi sharma; Apr 1st, 2009 at 4:04 pm.
Hi,
For the Control Values.. U need to convert your Bound Controls to TemplateFields. chk this
then you can FindControl these values.. in your .cs file
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>
Njoy koding... >>>
![]() |
Other Threads in the C# Forum
- Previous Thread: Reading in a .txt file, splitting it and then outputting the data to a listbox
- Next Thread: NEED HELP WITH Xpath Quries
Views: 592 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development drawing encryption enum event excel file files form format ftp function gcd gdi+ http httpwebrequest image index input java list listbox listener login mandelbrot math mouseclick mysql networking object oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view webbrowser windows winforms wpf xml





