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

Recommended Answers

All 5 Replies

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

Thx Very much,It worked...........

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

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

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

thx very much.......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.