Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
protected void Page_Load(object sender, EventArgs e)
{
    DataTable question_x = new DataTable("Question");
    question_x.Columns.Add(new DataColumn("ID", System.Type.GetType("System.UInt64")));
    question_x.Columns.Add(new DataColumn("Name"));
    question_x.Rows.Add(1, "1) Training deliver can be understood?");
    question_x.Rows.Add(1, "2) The way instructor deliver the training is satisfaction?");
    question_x.Rows.Add(1, "3) The notes are easy to understand?");
    question_x.Rows.Add(1, "4) The training room is appropriate?");
    question_x.Rows.Add(1, "5) The training time is satisfactory?");
    gvEdit.DataSource = question_x;
    gvEdit.DataBind();

}

ASP.NET
    <asp:GridView runat="server" CssClass="table table-striped table-bordered table-hover" ID="gvEdit" DataKeyNames="ID" AutoGenerateColumns="false" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Question" ItemStyle-ForeColor="White" ItemStyle-HorizontalAlign="Left" ItemStyle-BackColor="graytext" />
        <asp:TemplateField HeaderText="Poor">
            <ItemStyle HorizontalAlign="Center" />
            <ItemTemplate>
                <asp:RadioButton runat="server" ID="chkP" onclick="javascript:GridSelectAllColumn(this, 'chkPoor');" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Good">
            <ItemStyle HorizontalAlign="Center" />
            <ItemTemplate>
                <asp:RadioButton runat="server" ID="chkG" onclick="javascript:GridSelectAllColumn(this, 'chkGood');" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>



i have few survey question in my gridview,user can select one selection only for each question.I have difficulty in getting user selection in c#,please guide me on this thanks.. i want to have output something like this after i click submit button:

1. checkP
2. checkG
3. checkG
4. checkG
5. checkP

What I have tried:

please help,thanks in advance.
Posted
Updated 19-Jul-16 1:15am

C#
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chk = new CheckBox();
        chk.EnableViewState = true;
        chk.Enabled = true;
        chk.ID = "chkb";
        e.Row.Cells[1].Controls.Add(chk);
    }
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var chk = (CheckBox)e.Row.FindControl("chkb");
        // databind it here according to the DataSource in e.Row.DataItem
    }
}
 
Share this answer
 
While getting the value from grid view you have loop through each row of Grid view as
C#
foreach(GridViewRow row in GridView2.Rows)
{
    for(int i = 0; i < GridView2.Columns.Count; i++)
    {
        string header = GridView2.Columns[i].HeaderText;
        CheckBox chkp=(Checkbox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("chkp");
        CheckBox chkg=(Checkbox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("chkg");
       //Get the .checked values of chkp and chkg show it as you desire
       string Ans= chkp.checked==True?"CheckP":chkg.checked==True?"CheckG":"No Answer"
    }
}


Code might not compile as its not tested.
 
Share this answer
 
try this


<head>
    <script src="jquery.js"></script>
    <script>

        function GridSelectAllColumn(obj, val) {
            var $tr = $(obj).parent().parent();

            if (val == 'chkGood') {
                $("input[id*='chkG']", $tr)[0].checked = true;
                $("input[id*='chkP']", $tr)[0].checked = false;
            } else {
                $("input[id*='chkG']", $tr)[0].checked = false;
                $("input[id*='chkP']", $tr)[0].checked = true;
            }

        }

    </script>

</head>



C#
protected void ButtonSubmit_Click(object sender, EventArgs e)
       {
           foreach (GridViewRow row in gvEdit.Rows)
           {
               CheckBox chkG = (CheckBox)row.FindControl("chkG");
               CheckBox chkP = (CheckBox)row.FindControl("chkP");
               string question = row.Cells[0].Text;
               string selectedValue = chkG.Checked ? "CheckG" : chkP.Checked ? "CheckP" : "Not Selected";


           }
       }



Bind the data Inside !IsPostBack block

C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {

               DataTable question_x = new DataTable("Question");
               question_x.Columns.Add(new DataColumn("ID", System.Type.GetType("System.UInt64")));
               question_x.Columns.Add(new DataColumn("Name"));
               question_x.Rows.Add(1, "1) Training deliver can be understood?");
               question_x.Rows.Add(1, "2) The way instructor deliver the training is satisfaction?");
               question_x.Rows.Add(1, "3) The notes are easy to understand?");
               question_x.Rows.Add(1, "4) The training room is appropriate?");
               question_x.Rows.Add(1, "5) The training time is satisfactory?");
               gvEdit.DataSource = question_x;
               gvEdit.DataBind();
           }
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900