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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

additional checkboxes in gridview 1

Status
Not open for further replies.

russland

Programmer
Jan 9, 2003
315
CH
hi,

I'd like to fill a gridview with a list of all appartments in the database table. Now, in this list there are also appartments that do belong to the userID that has been passed by querystring. For those I want to check the checkbox. For all others not it's unchecked.

What's the best approach?

I tried with if-else-statements to set the checked-attribute of the checkbox to either true or false. Tough, I didn't even find out how to compare the values of the datasource (yes, the gridview is bound to the datasource). If-else statements are also not possible because then we would have 2 tags with the same name.

I'm a newbie and certainly not on the right track (since this should be such a little task).

Thanks for any help!
 
You will need to use the RowDataBound event of the gridview to compare values and check the checkbox as needed. You will need to have a column in the result set ( boolean or bit, or whatever) that designates that the current user belongs to the apartment. YOu can hide that column. Then as the row binds, if that flag is TRUE, for example, check the checkbox.

Jim
 
exactly what I tried, now. But how do I read the value of the RenterID, a BoundField? It reports an error NullReferenceException. Meaning it cannot set the reference. How do I read the value from column RenterID correctly?

thanks for your help, so far.

<code>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow)
{
//if( ((string)e.Row.FindControl("RenterID") == "1+){
Label l = (Label)e.Row.FindControl("RenterID");
if(l.Text == "1"){
((CheckBox)e.Row.FindControl("OwnedByRenter")).Checked = true;
}
}
}

</code>
 
you don't find a control unless it is in a template column. If it is not a templat column you just get the text of the cell e.row.cell(cell index).text
 
Okay, I couldn't see that in the documentation.


By the way. Do I have to be in edit mode make the checkboxes editable/checkable? (I don't really want the user to click an edit button - he should simply be able to click that checkbox when the gridview is rendered)

Thanks for the help... A star is on the way ;)
 
If you want the checkbox to always be editable then use the ItemTemplate of the column.
 
yes, that's what I did (forgot to set Enabled to true).
Today I'm playing Mr. Stupid.


<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="OwnedByRenter" runat="server" Enabled="true"/>
</ItemTemplate>
</asp:TemplateField>
 
benson

I saw that I would have to register the events in the page load method. This of course doesn't work since the OwnedByRenter-Checkbox is not directly in the form container. How do I register every checkbox in the rows of my gridview?

Thanks heaps.

EXAMPLE OF PROGRAMATIC EVENT REGISTERING
OwnedByRenter.CheckedChanged += new EventHandler(this.OwnedByRenter_OnCheckedChanged);

EXAMPLE OF DECLERATIVE CODE
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="OwnedByRenter" runat="server" Enabled="true" OnCheckedChanged="OwnedByRenter_OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
 
I used the autopostback attribute, which posts the entire form (gridview). hmm... not what i was looking for, but i think that will do it.

thanks heaps for your help all day long.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top