1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Grid View Help?????

Discussion in 'C#' started by rohansyco, Oct 13, 2009.

  1. #1
    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
     
    rohansyco, Oct 13, 2009 IP
  2. urstop

    urstop Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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?
     
    urstop, Oct 15, 2009 IP
  3. geekstrack

    geekstrack Member

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    your code is looking fine tell me where you are getting any error?
     
    geekstrack, Feb 23, 2012 IP
  4. ivickon

    ivickon Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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);
    }
    }
    <asp:panel 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>
    </asp:panel>
    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!
     
    ivickon, Feb 25, 2012 IP
  5. abhishekabhi

    abhishekabhi Greenhorn

    Messages:
    57
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #5
    Gridview display data in webpage .
    We can do Insert, Update ,delete ,add control , paging, in Gridview .
     
    abhishekabhi, May 31, 2012 IP
  6. krishna1987

    krishna1987 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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 !
     
    krishna1987, Jun 9, 2012 IP