9 Replies - 2077 Views - Last Post: 03 April 2009 - 01:17 PM Rate Topic: -----

#1 mansi sharma  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 19-February 09

Gridview + Checkboxes

Posted 31 March 2009 - 11:05 PM

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

<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>





Help with Code Tags
(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..

Is This A Good Question/Topic? 0
  • +

Replies To: Gridview + Checkboxes

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Gridview + Checkboxes

Posted 01 April 2009 - 07:44 AM

something like this...

for (int i = 0; i < DataGrid1.Rows.Count; i++)
{
     DataGridRow row = DataGrid1.Rows[i];
     CheckBox cb = row.FindControl("chk"); // Finding the ItemTemplate checkbox.
     if (cb.Checked)
     {
            // Do whatever code for rows that are checked
      }
}



I don't have my code with me, so I am trying to do it from memory.
Was This Post Helpful? 0
  • +
  • -

#3 mansi sharma  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 19-February 09

Re: Gridview + Checkboxes

Posted 01 April 2009 - 10:08 AM

hi eclipsed4utoo,there is no DataGridRow type(line 2).But thx for the reply,I got the solution-

Just see the bold part in coding.
protected void Page_Load(object sender, EventArgs e)
	{
	   [b] [color=#FF0000]if (!IsPostBack )[/color][/b]		
		 {

			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();
	   }
	}


This post has been edited by mansi sharma: 01 April 2009 - 10:09 AM

Was This Post Helpful? 1

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Gridview + Checkboxes

Posted 01 April 2009 - 10:18 AM

cool.

This post has been edited by eclipsed4utoo: 01 April 2009 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 mansi sharma  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 19-February 09

Re: Gridview + Checkboxes

Posted 01 April 2009 - 12:01 PM

hi elipsed, 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...

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
<asp:BoundField HeaderText="Roll" DataField ="roll" />
							<asp:BoundField HeaderText="Name"  DataField ="name"/>
							<asp:BoundField HeaderText="Marks" DataField ="marks"/>


Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Gridview + Checkboxes

Posted 01 April 2009 - 12:23 PM

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)
            {
                [size=4][b]string name = GridView1.Rows[i].Cells[2].Value.ToString();[/b][/size]

                Label lblName = (Label)GridView1.FindControl("Name");
                sName = lblName.ToString;
               
            }

           
        }
        

    }



remember that the checkbox column does count as a column. So you are actually looking for the 3rd column's data.

This post has been edited by eclipsed4utoo: 01 April 2009 - 12:24 PM

Was This Post Helpful? 0
  • +
  • -

#7 mansi sharma  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 19-February 09

Re: Gridview + Checkboxes

Posted 03 April 2009 - 11:26 AM

Hi elipsed,thx for the reply Bt its not working...

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)
			{
				string name = GridView1.Rows[i].Cells[2].value.tostring();
							 
			   
			}

		   
		}
		



ERROR-
'System.Web.UI.WebControls.TableCell' does not contain a definition for 'value'	


Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Gridview + Checkboxes

Posted 03 April 2009 - 12:50 PM

it is either...

GridView1.Rows[i].Cells[2].Value.ToString();



or

GridView1.Rows[i].Cells[2].Text;


Was This Post Helpful? 0
  • +
  • -

#9 mansi sharma  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 19-February 09

Re: Gridview + Checkboxes

Posted 03 April 2009 - 01:01 PM

he elipsed,Thx..Ur code is working..Other Method to get GridView cell value that i do..

SOURCE TAB
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
						<Columns>
						<asp:TemplateField >
						<ItemTemplate >
						<asp:CheckBox ID ="chk" runat ="server" />
						</ItemTemplate>
						</asp:TemplateField>
						<asp:TemplateField  HeaderText="Roll" >
						<ItemTemplate > 
						<asp:Label runat ="server" ID="rollbind" Text='<%# Bind("roll") %>'>
						</asp:Label>
						 </ItemTemplate>
						 </asp:TemplateField>
						   <%-- <asp:BoundField HeaderText="Roll" DataField ="roll" />--%>
							<asp:BoundField HeaderText="Name"  DataField ="name"/>
							<asp:BoundField HeaderText="Marks" DataField ="marks"/>
						</Columns>
					</asp:GridView>



protected void Button1_Click(object sender, EventArgs e)
	{
		string sRoll;
		bool bchecked;

		for (int i = 0; i < this.GridView1.Rows.Count; i++)
		{
			bchecked = ((CheckBox)GridView1.Rows[i].FindControl("chk")).Checked;
			if (bchecked == true)
			{
				//FIRST METHOD
				//sRoll = ((Label)GridView1.Rows[i].FindControl("rollbind")).Text;
				//SECOND METHOD
				sRoll = GridView1.Rows[i].Cells[2].Text; 
  
			   
			}

		   
		}





Hey i want to ask that there is no any option here in this site..to mention that this thraed is solved as in daniwebb
Was This Post Helpful? 0
  • +
  • -

#10 mansi sharma  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 19-February 09

Re: Gridview + Checkboxes

Posted 03 April 2009 - 01:17 PM

hi fnd, i want to ask u one thing more,
If I use ur code
sRoll = GridView1.Rows[i].Cells[1].Text;

It doesn't return any value for first column,for rest columns right values are returning ..Y so????????

Code that i have used...returnung the data of first column also...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1