any way to change a control inside a grid?

Y

yoni

Hi,
i got a grid on an asp.net (2.0) page . i want, in a certain field,
for certain rows to show a combobox, but in other cases show a
checkbox (basically, if i got 1 record or less to show in the combo, i
want just a checkbox) anybody has any idea how it can be done? (best
if anybody knows some article about it, with code snippet of some
sort... vb or c#, doesnt matter)

thanks!
Jonathan
 
G

Guest

You can utilize Template Fields / Columns to do just that. Define both of
your controls inside the template:

<asp:TemplateField HeaderStyle-Width="20px">
<ItemTemplate>
<asp:DropDownList id="ddl" runat="server" />
<asp:CheckBox id="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>

and then make one or another invisible, based on your requirements:

private void grd_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
TableCell templateCell = row.Cells[_TEMPLATE_COLUMN_INDEX];
if (_ddlControlIndex == -1)
{
_ddlControlIndex = Ctls.GetCellControlIndex(templateCell ,
typeof(DropDownList));
_chkControlIndex = Ctls.GetCellControlIndex(templateCell ,
typeof(CheckBox));
}
((DropDownList)templateCell .Controls[_ddlControlIndex]).Visible =
getDropDownVisibleLogic();
((CheckBox)templateCell .Controls[_chkControlIndex]).Visible =
getCheckBoxVisibleLogic();

}
}

HTH
 
Y

yoni

You can utilize Template Fields / Columns to do just that. Define both of
your controls inside the template:

<asp:TemplateField HeaderStyle-Width="20px">
<ItemTemplate>
<asp:DropDownList id="ddl" runat="server" />
<asp:CheckBox id="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>

and then make one or another invisible, based on your requirements:

private void grd_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
TableCell templateCell = row.Cells[_TEMPLATE_COLUMN_INDEX];
if (_ddlControlIndex == -1)
{
_ddlControlIndex = Ctls.GetCellControlIndex(templateCell ,
typeof(DropDownList));
_chkControlIndex = Ctls.GetCellControlIndex(templateCell ,
typeof(CheckBox));
}
((DropDownList)templateCell .Controls[_ddlControlIndex]).Visible =
getDropDownVisibleLogic();
((CheckBox)templateCell .Controls[_chkControlIndex]).Visible =
getCheckBoxVisibleLogic();

}

}

HTH



Hi,
i got a grid on an asp.net (2.0) page . i want, in a certain field,
for certain rows to show a combobox, but in other cases show a
checkbox (basically, if i got 1 record or less to show in the combo, i
want just a checkbox) anybody has any idea how it can be done? (best
if anybody knows some article about it, with code snippet of some
sort... vb or c#, doesnt matter)
thanks!
Jonathan- Hide quoted text -

- Show quoted text -

that worked like magic! thanks very much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top