473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

RadioButtonList in a DataGrid

Anyone have any pointers on how to set the Value and Selected attributes in
a ListItem in a RadioButtonList that is in a DataGrid?

Here's what I have
------DataGrid------
-- BoundColumn 0 --
-- BoundColumn 1 --
-- BoundColumn 2 --
-- TemplateColumn 4 --
-- RadioButtonList --
ListItem 0 // need to set the Value and Selected attributes from
DataSet Bound to DataGrid

TIA,
George
Nov 18 '05 #1
5 5490
Oh, and this is the error message I'm getting:

Compiler Error Message: CS0117: 'System.Web.UI.WebControls.ListItem' does
not contain a definition for 'DataBinding'

-g

"DotNetGruven" <ms********@javagruven.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Anyone have any pointers on how to set the Value and Selected attributes in a ListItem in a RadioButtonList that is in a DataGrid?

Here's what I have
------DataGrid------
-- BoundColumn 0 --
-- BoundColumn 1 --
-- BoundColumn 2 --
-- TemplateColumn 4 --
-- RadioButtonList --
ListItem 0 // need to set the Value and Selected attributes from DataSet Bound to DataGrid

TIA,
George

Nov 18 '05 #2
You should be able to DataBind to the List, but not the List Item.

If you want to just set the value on the ListItem, give the
RadioButtonList an id (i.e., rblList) and code the OnDataBindingEvent
for the DataGrid something like:

//set list item
RadioButtonList myList = (RadioButtonList) e.Item.FindControl("rblList");
myList.Items[0].Value = <value>;
myList.Items[0].Selected=true/false;

OR
//Bind radiobuttonlist
RadioButtonList myList = (RadioButtonList) e.Item.FindControl("rblList");
myList.DataSource = yourSource;
myList.DataBind();

Hope this helps
DotNetGruven wrote:
Oh, and this is the error message I'm getting:

Compiler Error Message: CS0117: 'System.Web.UI.WebControls.ListItem' does
not contain a definition for 'DataBinding'

-g

"DotNetGruven" <ms********@javagruven.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Anyone have any pointers on how to set the Value and Selected attributes


in
a ListItem in a RadioButtonList that is in a DataGrid?

Here's what I have
------DataGrid------
-- BoundColumn 0 --
-- BoundColumn 1 --
-- BoundColumn 2 --
-- TemplateColumn 4 --
-- RadioButtonList --
ListItem 0 // need to set the Value and Selected attributes


from
DataSet Bound to DataGrid

TIA,
George


Nov 18 '05 #3
Hi George,
Thanks for posting in the community! My name is Steven, and I'll be
assisting you on this issue.
From your description, you has a DataGrid which contains a template column
with a radiobuttonlist in it.
Also you want to set the radionbuttonlist 's item value and select
attribute at runtime during
the time when DataGrid's column is being bind to the data retrieved from
database, yes?
If there is anything I misunderstood, please feel free to let me know.
As for this problem, I think we can use the DataGrid's DataItemBound event
to implement our requirement. In the DataItemBound event, we can retrieve
the certain row which is currently being bund with datas from the
DataSource. Also, we can retrieve the certain row of DataSource. For our
siutation, we want to do some modification on the radiobuttonlist in the
template collumn, we can first retrieve the RadioButtonList control from
the column using "FindControl" and then do operations on it. For example:

private void dgMain_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
System.Web.UI.WebControls.RadioButtonList lst =
(System.Web.UI.WebControls.RadioButtonList)e.Item. Cells[2].FindControl("rblB
ind");
lst.Items[0].Text = drv["grade"].ToString();
lst.Items[0].Value = drv["grade"].ToString();
lst.SelectedIndex = 0;
}
}

#dgMain is the DataGrid in a page

To make such approach clearly, here is a sample page I've made, you can
refer to it if you have anything unclear in my above description:
---------------------------------aspx page----------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>RadioButtonList</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td>
<asp:DataGrid id="dgMain" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="index"
HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="name"
HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="BoundList">
<ItemTemplate>
<asp:RadioButtonList id="rblBind" runat="server">
<asp:ListItem Value="BindItem">BindItem</asp:ListItem>
<asp:ListItem Value="ItemA">ItemA</asp:ListItem>
<asp:ListItem Value="ItemB">ItemB</asp:ListItem>
<asp:ListItem Value="ItemC">ItemC</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>

-----------------------------code behind page class------------------
public class RadioButtonList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgMain;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
BindGrid();
}
}

protected void BindGrid()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("grade");

for(int i=0;i<15;i++)
{
int index = i+1;
DataRow newrow = tb.NewRow();
newrow["index"] = index.ToString();
newrow["name"] = "name" + index.ToString();
switch(index%3)
{
case 0:
newrow["grade"] = "low";
break;
case 1:
newrow["grade"] = "normal";
break;
case 2:
newrow["grade"] = "high";
break;
}

tb.Rows.Add(newrow);
}

dgMain.DataSource = tb;
dgMain.DataBind();

}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dgMain.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler (this.dgMain_ItemDataBound
);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgMain_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
System.Web.UI.WebControls.RadioButtonList lst =
(System.Web.UI.WebControls.RadioButtonList)e.Item. Cells[2].FindControl("rblB
ind");
lst.Items[0].Text = drv["grade"].ToString();
lst.Items[0].Value = drv["grade"].ToString();
lst.SelectedIndex = 0;

}
}
}
----------------------------------------------------------------------------
-------------------------

If you have any further questions, please feel free to post here.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 18 '05 #4
Thanks Ruprict, your help below set me down the right path to the answer. Here is what worked:

public void OnItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType listItemType = e.Item.ItemType;
if ( listItemType == ListItemType.AlternatingItem || listItemType == ListItemType.Item )
{
TableCell cell;
cell = (TableCell) e.Item.Cells[5];
if ( cell.Controls[1]is RadioButtonList && e.Item.DataItem is DataRowView )
{
RadioButtonList rbl = (RadioButtonList)cell.Controls[1];
DataRowView dr = (DataRowView)e.Item.DataItem;
rbl.Items[0].Selected = Convert.ToBoolean(dr.Row.ItemArray[5]);
rbl.Items[1].Selected = !rbl.Items[0].Selected;
rbl.Items[0].Value = rbl.Items[1].Value = dr.Row.ItemArray[0].ToString();
}
}
}

"Ruprict" <ru*****@bellsouth.net> wrote in message news:cT*****************@bignews6.bellsouth.net...
You should be able to DataBind to the List, but not the List Item.

If you want to just set the value on the ListItem, give the
RadioButtonList an id (i.e., rblList) and code the OnDataBindingEvent
for the DataGrid something like:

//set list item
RadioButtonList myList = (RadioButtonList) e.Item.FindControl("rblList");
myList.Items[0].Value = <value>;
myList.Items[0].Selected=true/false;

OR
//Bind radiobuttonlist
RadioButtonList myList = (RadioButtonList) e.Item.FindControl("rblList");
myList.DataSource = yourSource;
myList.DataBind();

Hope this helps


DotNetGruven wrote:
Oh, and this is the error message I'm getting:

Compiler Error Message: CS0117: 'System.Web.UI.WebControls.ListItem' does
not contain a definition for 'DataBinding'

-g

"DotNetGruven" <ms********@javagruven.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Anyone have any pointers on how to set the Value and Selected attributes


in
a ListItem in a RadioButtonList that is in a DataGrid?

Here's what I have
------DataGrid------
-- BoundColumn 0 --
-- BoundColumn 1 --
-- BoundColumn 2 --
-- TemplateColumn 4 --
-- RadioButtonList --
ListItem 0 // need to set the Value and Selected attributes


from
DataSet Bound to DataGrid

TIA,
George



Nov 18 '05 #5
Hi George,
Thanks for your followup. I'm glad that the problem has been resolved. In
the meantime, if you have any further questions or need any further help,
please feel free to let me know.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: juststarter | last post by:
Hello, I have an aspx file where i've put a placeholder element. On load i create dynamically a table which contains a checkbox and a radiobuttonlist in each tablerow . The radiobuttonlist...
0
by: Empire City | last post by:
I have an ASP.NET form with a DataGrid and Button. I want to put a RadioButtonList in a DataGrid cell. I bind it to an ArrayList which has a ListItem in the cell. The display part works fine. I...
0
by: Bob Hoke | last post by:
I am trying to do some of the things we used to do in classic ASP that required some pretty ugly loops in the .ASP page. I have two different DataSets that each have a DataView that I can change...
1
by: Tim | last post by:
I have a RadioButtonList inside the EditItemTemplate of a Datagrid. On the Edit command of the datagrid I am calling the following command: public void dgNodeBranches_Edit(object sender,...
6
by: DotNetGruven | last post by:
I have a webform that has a DataGrid on it with a RadioButtonList in each row. It is a simple On & Off. When the User Clicks on either of the RadioButtons, I need to postback to the server and...
0
by: Luis Esteban Valencia | last post by:
http://www.codeproject.com/aspnet/DataGridCCEvents.asp#xx1009236xx Read this first and see if you can help me have tried but the Intelisense of the radiobuttonlist doestn have the event...
2
by: Brian | last post by:
I have a datagrid that is listing a bunch of questions from my DB.. I am creating a radiobuttonlist for each question to give me a yes or no answer. I would like to use the "QuestionCode" column...
0
by: ad | last post by:
I have a tabe with a filed SexID, it use 1 for Male and 2 for Female. I want to let user can edit this in a DataGrid with a RadioButtonList. I add a RadioButtonList in the EditItemTemplate:...
0
by: rcoco | last post by:
Hi, Ihave a datagrid that Insert data in some rows. One column has RadioButtonList that contains three Items. But When I select one radiobuton, insert data in other columns and select insert data...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.