C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » ASP.NET »

how to add a value from a gridview with radio button and that value was selected by defau


Posted Date: 21 Nov 2008      Posted By: Spha      Member Level: Silver     Points: 1   Responses: 5



Hi guys i have a problem here now i got my gridview to check the radio button of the first value on the gridview but now my roblem is that now,when a user clicks on submit button that selected value needs to be stored onto the DB,can you pls show me on how to read the value of that first default selected record and mabe put it into the label,then i can take it from there.the code that i used to check the radio button of the first record is as ff:

for (int i = 0; i <= gvExpense.Rows.Count - 1; i++)
{
RadioButton rd = new RadioButton();
rd = (RadioButton)gvExpense.Rows[i].FindControl("rb1");

if (i == 0)
{
rd.Checked = true;


}

else
{
rd.Checked = false;
}

}

Pls guyz help





Responses

Author: greeny_1984    21 Nov 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 4

in the same way as above

on submit_click event
for (int i = 0; i <= gvExpense.Rows.Count - 1; i++)
{
RadioButton rd = new RadioButton();
rd = (RadioButton)gvExpense.Rows[i].FindControl("rb1");

if (i == 0)
{
//get u r record value as shown below

string str = gvExpense.Rows[i].Cells[0].Text;

}

else
{
rd.Checked = false;
}

}

Regards,
Greeny_1984

Rate this Response[Excellent/Good/Poor]
FRESHERS check this link
Need help from me join here



Author: Praveen    21 Nov 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 6

if u want to get from database and set the grid radio button value u need to write in
gridview1_DatarowBound Event like this

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
RadioButton rb = (RadioButton)(e.Row.FindControl("rb"));
if (rb != null)
rb.Checked = false;
}

and to retrive the satus of radio button at button click

protected void btn1_Click(object sender, EventArgs e)
{
RadioButton rb = null;
foreach (GridViewRow grv in GridView1.Rows)
{
rb = (RadioButton)(grv.FindControl("rb"));

}
}


hope this will help u



Author: Spha    21 Nov 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 6

this is the code i used guys and when it does'nt return a value of that selected record by default any idea why? here is the code

for (int i = 0; i <= gvExpense.Rows.Count - 1; i++)
{
RadioButton rd = new RadioButton();
rd = (RadioButton)gvExpense.Rows[i].FindControl("rb1");

if (i == 0)
{
// rd.Checked = true;
string str = gvExpense.Rows[i].Cells[0].Text;
lblCode.Text = Convert.ToString(str);

dbSE.InsertStudentModule(Convert.ToInt32(123), Convert.ToString(lblCode.Text), "no desc", 1, 1, "PMB", "C", 2009, Convert.ToDateTime(DateTime.Now));


}

else
{
rd.Checked = false;
}

}


and this is how my Gridview looks like on Html with the binding

<asp:GridView ID="gvExpense" runat="server" AutoGenerateColumns="False" CellPadding="1"
EmptyDataText="No compulsory have been listed" Font-Names="Arial" Font-Size="8pt"
ForeColor="DimGray" Height="63px" PageSize="15" Width="525px" OnRowCommand="gvExpense_RowCommand">
<Columns>

<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label ID="lblModCode" runat="server" Text='<%# Eval("DGREDEGREECODE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label ID="lblModDesc" runat="server" Text='<%# Eval("DGREDEGREECODE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>



<asp:BoundField DataField="DGREDEGREECODE" HeaderText="DGREDEGREECODE" SortExpression="DGREDEGREECODE"
Visible="False" />
<asp:BoundField DataField="Degree" SortExpression="Degree" />

<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="rb1" runat="server" AutoPostBack="True" OnCheckedChanged="rb1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>


<asp:TemplateField>
<ItemTemplate>
<a href="javascript:Openpopup('./Popup/Degree/PopUp.aspx?DegreeCode=<%# Eval("DGREDEGREECODE") %>')">
<asp:Label ID="Label12" runat="server" ForeColor="steelblue" Text="Learn More..."></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>






</Columns>
<HeaderStyle BackColor="WhiteSmoke" />
<AlternatingRowStyle BackColor="GhostWhite" />
</asp:GridView>



Author: Legend     21 Nov 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 4

on submit_click event
for (int i = 0; i <= gvExpense.Rows.Count - 1; i++)
{
RadioButton rd = new RadioButton();
rd = (RadioButton)gvExpense.Rows[i].FindControl("rb1");

if (i == 0)
{
//get u r record value as shown below

string str = gvExpense.Rows[i].Cells[0].Text;

}

else
{
rd.Checked = false;
}

}



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Adding Default selected value from a gridview with radiobutton C#
Previous : socket using asp.net 2.0 for client - server communication
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use