Search code examples
asp.netgridviewdatasetradiobuttonlist

how to make radiobutton selected in gridview depend on value return from dataset?


i have web application in which i used gridview. In gridview i used radiobutton list this radiobutton list has two item Yes and No.i want to select one item from these two depend on value i get from dataset at the time binding.suppose my column name is is_selected and it returns True then in radiobutton list Yes should be checked. This is code i have tried myself but no success,

 <asp:TemplateField HeaderText="Ready To Join?" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>


                   <asp:RadioButtonList ID="rbd_join" AutoPostBack="true" runat="server" 
                            RepeatDirection="Horizontal" BorderStyle="None" BorderWidth="0px" BorderColor="Transparent"
                            onselectedindexchanged="rbd_join_SelectedIndexChanged" 
                            DataValueField="is_selected">
                        <asp:ListItem Text="Yes" Value="1" ></asp:ListItem>
                        <asp:ListItem Text="No" Value="0"></asp:ListItem>
                        </asp:RadioButtonList>
                        </ItemTemplate>

<ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:TemplateField>

After Edit

Addition

.cs file on button click event this is code:

Dataset  ds=new Dataset();
ds=bindgrid();
if(ds.table[0].rows.count>0)
{
    grd_view.Datasource=ds.tables[0];
    grd_view.Databind();
}
public Dataset bindgrid()
{
    SqlParameter stud_ID = new SqlParameter("@student_id", SqlDbType.Int);
                stud_ID.Value = 1;
                SqlCommand cmdSql = new SqlCommand();
                cmdSql.CommandType = CommandType.StoredProcedure;
                cmdSql.CommandText = "usp_select_candidate_inplacement_byAdmin";
                cmdSql.Parameters.Add(stud_ID);
                DataSet ds = new DataSet();
                DataClass dl = new DataClass();
                ds = dl.dsSelect(cmdSql);
                return ds;
}

and this is event i added

protected void grd_view_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var radioList = e.Row.FindControl("rbd_join") as RadioButtonList;
                var myobject = e.Row.DataItem as DataRow;
                bool is_sel = bool.Parse(myobject ["is_selected"].ToString());

                radioList.SelectedValue = is_sel ? "1" : "0";

            }
}

Solution

  • You need to use the RowDataBound event:

    ASPX

    <asp:GridView ..... OnRowDataBound="gv_RowDataBound"
    

    Code behind

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var radioList = e.Row.FindControl("rbd_join") as RadioButtonList;
    
            // this will be the object that you are binding to the grid
            var myObject = e.Row.DataItem as DataRowView;
            bool isSelected = bool.Parse(myObject["is_selected"].ToString());
    
            radioList.SelectedValue = isSelected ? "1" : "0";
        }
    }