Hi, i have added a control to my grid view as follows::: for (int i = 0; i < GridView2.Rows.Count; i++) { RadioButtonList RBL = new RadioButtonList(); RBL.Items.Add("Present"); RBL.Items[0].Value = "Present"; RBL.Items.Add("Absent"); RBL.Items[1].Value = "Absent"; RBL.Items.Add("HalfDay"); RBL.Items[2].Value = "HalfDay"; RBL.RepeatDirection = RepeatDirection.Horizontal; string CurrentStatus = GridView2.Rows.Cells[2].Text.ToString(); switch (CurrentStatus) { case "Present": RBL.SelectedIndex = 0; break; case "Absent": RBL.SelectedIndex = 1; break; case "HalfDay": RBL.SelectedIndex = 2; break; } GridView2.Rows.Cells[2].Controls.Clear(); GridView2.Rows.Cells[2].Controls.Add(RBL); } and then i try to get the selected value from it as: for (int i = 0; i < GridView2.Rows.Count; i++) { Control ctrl = GridView2.Rows.Cells[2].FindControl("radiobtn") ; RadioButtonList RL = ctrl as RadioButtonList; ds.Tables[0].Rows[2] = RL.SelectedValue; Label2.Text += string.Format("{0} --- {1}, </br> ", i, RL.SelectedItem); } but i m not getting the value please help me out... Thanks, Rohan
Where are you trying to get the value back? Is it in the same method where you are adding the controls or from a differnt method/event handler? Are you getting any Null reference error or it just does not return the selected Index?
GridView s RowCreated event. The RowCreated event fires once for every row added, whether or not the data is being rebound to the GridView. That means that even on a postback when the data is reloaded from view state, the RowCreated event still fires and this is the reason we are using it instead of RowDataBound (which fires only when the data is explicitly bound to the data Web control). In this event handler, we only want to proceed if we re dealing with a data row. For each data row we want to programmatically reference the RadioButtonMarkup Literal control and set its Text property to the markup to emit. As the following code shows, the markup emitted creates a radio button whose name attribute is set to SuppliersGroup, whose id attribute is set to RowSelectorX, where X is the index of the GridView row, and whose value attribute is set to the index of the GridView row. protected void Suppliers_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // Grab a reference to the Literal control Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup"); // Output the markup except for the "checked" attribute output.Text = string.Format( @"<input type="radio" name="SuppliersGroup" " + @"id="RowSelector{0}" value="{0}" />", e.Row.RowIndex); } } private int SuppliersSelectedIndex { get { if (string.IsNullOrEmpty(Request.Form["SuppliersGroup"])) return -1; else return Convert.ToInt32(Request.Form["SuppliersGroup"]); } } protected void Suppliers_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // Grab a reference to the Literal control Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup"); // Output the markup except for the "checked" attribute output.Text = string.Format( @"<input type="radio" name="SuppliersGroup" " + @"id="RowSelector{0}" value="{0}"", e.Row.RowIndex); // See if we need to add the "checked" attribute if (SuppliersSelectedIndex == e.Row.RowIndex) output.Text += @" checked="checked""; // Add the closing tag output.Text += " />"; } } protected void SendToProducts_Click(object sender, EventArgs e) { // Send the user to ~/Filtering/ProductsForSupplierDetails.aspx int supplierID = Convert.ToInt32(Suppliers.DataKeys[SuppliersSelectedIndex].Value); Response.Redirect( "~/Filtering/ProductsForSupplierDetails.aspx?SupplierID=" + supplierID); } } protected void SendToProducts_Click(object sender, EventArgs e) { // make sure one of the radio buttons has been selected if (SuppliersSelectedIndex < 0) ChooseSupplierMsg.Visible = true; else { // Send the user to ~/Filtering/ProductsForSupplierDetails.aspx int supplierID = Convert.ToInt32(Suppliers.DataKeys[SuppliersSelectedIndex].Value); Response.Redirect( "~/Filtering/ProductsForSupplierDetails.aspx?SupplierID=" + supplierID); } } <aspanel runat="server" ID="ProductsBySupplierPanel" Visible="False"> <h3> Products for the Selected Supplier</h3> <p> <asp:GridView ID="ProductsBySupplier" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="ProductsBySupplierDataSource" EnableViewState="False"> <Columns> <asp:BoundField DataField="ProductName" HeaderText="Product" SortExpression="ProductName" /> <asp:BoundField DataField="CategoryName" HeaderText="Category" ReadOnly="True" SortExpression="CategoryName" /> <asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" HeaderText="Price" HtmlEncode="False" SortExpression="UnitPrice" /> </Columns> </asp:GridView> <asp:ObjectDataSource ID="ProductsBySupplierDataSource" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetProductsBySupplierID" TypeName="ProductsBLL"> <SelectParameters> <asp:ControlParameter ControlID="Suppliers" Name="supplierID" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </asp:ObjectDataSource> </p> </aspanel> protected void ListProducts_Click(object sender, EventArgs e) { // make sure one of the radio buttons has been selected if (SuppliersSelectedIndex < 0) { ChooseSupplierMsg.Visible = true; ProductsBySupplierPanel.Visible = false; } else { // Set the GridView's SelectedIndex Suppliers.SelectedIndex = SuppliersSelectedIndex; // Show the ProductsBySupplierPanel panel ProductsBySupplierPanel.Visible = true; } } Having explored how to add a column of radio buttons, let us turn our attention to adding a column of checkboxes. With a column of checkboxes, a user can select one or more GridView rows and then perform some operation on all of the selected rows (such as selecting a set of emails from a web-based email client, and then choosing to delete all selected emails). In the next tutorial we ll see how to add such a column. Happy Programming!
Gridview display data in webpage . We can do Insert, Update ,delete ,add control , paging, in Gridview .
just use the debugging process by which you will get the point where the value is not being inserted by doing such a activity you can easily get the desired results let you try hurrahaaaa !