Radio button in gridview

0

Good morning everyone.

I need to create an online form that will have issues that will be entered daily into each user's profile. All questions are stored in a database along with their alternatives and correct answer.

So I can set everything right, I bring the questions and I mount the gridview with the radios button. However at the time of saving the answers I need to pick the dd dd grid line options and get which radio is selected.

Follow the form code:

<form id="form1" runat="server">
<div>
<asp:GridView ID="gridQuestoes" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField ="questao" HeaderText="questao" />
        <asp:BoundField DataField ="Prioridade" HeaderText="Prioridade" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:RadioButton ID="rbtSim" runat="server" Text="Sim" GroupName="users"/>
                <asp:RadioButton ID="rbtNao" runat="server" Text="Não" GroupName="users"/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
</div>
    <asp:Button ID="btnGravar" runat="server" Text="GRAVAR" OnClick="btnGravar_Click" />
</form>

Code used to populate GridView

BancoDeDados bd = new BancoDeDados();
        List<Modelos.Questoes> lista = bd.listarQuestoes(1);
        List<Quest1> listarQuest = new List<Quest1>();



        foreach (Modelos.Questoes questoes in lista)
        {
            if (questoes.tipo == 1)
            {
                listarQuest.Add(new Quest1()
                {
                    questao = questoes.questao,
                    prioridade = questoes.prioridade
                });                    
            }
        }
        gridQuestoes.DataSource = listarQuest;
        gridQuestoes.DataBind();

In some searches I made this code to verify but it is not working

foreach (GridViewRow row in gridQuestoes.Rows)
        {
            BancoDeDados bd = new BancoDeDados();
            RadioButton rb1 = row.FindControl("rbtSim") as RadioButton;
            RadioButton rb2 = row.FindControl("rbtNao") as RadioButton;
            if (rb1.Checked == true)
            {                    
                bd.gravar("Sim");
            }
            else if (rb2.Checked == true)
            {                    
                bd.gravar("Não");
            }
        }
    
asked by anonymous 13.07.2016 / 13:36

1 answer

0

Good morning.

I was able to solve my problem this way I do not know if it is the best way but it works if someone wants to use it or has another way

I added one action each time one of the buttons is activated.

 <asp:TemplateField>
            <ItemTemplate>
                <asp:RadioButton ID="rbtSim" runat="server" Text="Sim" GroupName="users" OnCheckedChanged="rbtSim_CheckedChanged" AutoPostBack="true"/>
                <asp:RadioButton ID="rbtNao" runat="server" Text="Não" GroupName="users" OnCheckedChanged="rbtNao_CheckedChanged" AutoPostBack="true"/>
            </ItemTemplate>
        </asp:TemplateField>

and in the actions I used this function

 protected void rbtSim_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton rbSim = (RadioButton)sender;
        GridViewRow row = (GridViewRow)rbSim.Parent.Parent;

        foreach (Quest1 q in listarQuest)
        {
            if (row.RowIndex == q.id)
            {
                q.resp = "Sim";
            }
        }            
    }

I hope it can help someone and if someone has a different resolution method.

att

    
15.07.2016 / 16:23