Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bind a checkbox to a gridview 1

Status
Not open for further replies.

tekkerguy

Programmer
Nov 16, 2005
196
US
I've got a webpage which is pulling up a table from sql server into a datatable in the codebehind of the main page.

It displays the columns correctly, however I want to have a checkbox next to each row so that the user can check off the rows he wants to have copied to a database on another server.

How do I add this to the grid?

I've added a <asp:checkboxfield> but believe that refers to a column in the database, and not a separate checkbox.
 
That worked perfectly, now my second question is this:

my gridview is loaded from the datatable.

I've placed a checkbox Item and a submit button in the form containing the gridview.

what I want to happen is, if the user clicks on say 2 out of three checkboxes in the gridview rows, then submits, I want the program to take each selected row and copy it into a new database.

how do I go about only updating the rows the user selected. If there is an example on the web, or if you know, I'd greatly appreciate it.

 
When you click on the button, loop through each row of the datagrid. You get a reference to the check box using FindControl("YourCBName") and check the checked property. If it is true, do what you need to do.
 
Actually it's not working. You're saying Datagrid, I'm working with a gridview.

Gridview won't let me add a template column and then add a checkbox in it. Also, gridview won't let me give a column an ID so findcontrol can't find anything.

Is there another way to do this, or am I doing it wrong?
 
A Gridview works the same way and you can add template columns to a gridview. FindControl works by passing the control name to it. Use it in the RowDataBound event.
 
When I try to use the rowdatabound, it says that the aspx page

does not contain a definition for 'CustomersGridView_RowDataBound'

I created the customergridview_rowdatabound funcition in the c# pagebehind.

if I create it in the aspx page itself, it works, but it doesn't set the checkbox to true when I've checked it off.

is this because the gridview is bound in the pagebehind?

I assume it's because it's running the rowdatabound before the gridview is created, therefore nothing is there to bind.
 
The rowdatabound events fires for EACH row that is bound to the datasource, eg. If you have 10 rows in your datasource, the rowdatabound event will fire 10 times, once for each row in the datasource.
 
Not sure that answers my question. Rowdatabound event is not firing at all. When I run the aspx page, I get the above error message

"page.aspx does not contain a definition for 'CustomersGridView_RowDataBound'"

even though the CustomersGridView_RowDataBound function is defined in the code behind.



However when I put it directly on the aspx page in a script, it fires, but it's not catching the checkbox values, as 1 is checked yet is still does nothing.

Should this not be working from the code behind?
 
I would do a test and create a new page that uses the code behind page. And just add a simple grid and test to make sure the rowdatabound event fires.
 
First, thanks for all your help!

Now I created a test page, and it worked.

So the only difference between the test page and the main page, is the test page used a sqlconnection to get the data, where my actual page is using a datatable as the datasource, and it's being initialized in the c# code

Code:
ProClarityLibrary _library = new ProClarityLibrary();
_dt = _library.Library;
GridView1.DataSource = _dt;
GridView1.DataBind();

Is there something else I need to set to allow it to bind the check box?

Perhaps I need to set the key column or something?



 
In your orginal page, I am guessing you are not getting any rows back from your code that you show above. I would look into that as your problem.
 
I am getting rows back. I'm pulling the data from a sql server, into a datatable, using a stored procedure. then binding that datatable to gridview1. It populates the table fine when the page is executed, but it just can't see the value of the checkbox. Actually, I don't think that it sees the checkbox at all since it doesn't even recognize the Text tag.
 
I Fixed it!!!

Thanks again for your help jbenson!

Your info helped me to get the checkbox set, the other problem was I didn't have a

Code:
if(!page.ispostback)

on my page load, so everytime I hit the submit button, it cleared out all the bindings.

Put that in, and everything is great now!

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top