Radio button in gridview

Asked

Viewed 701 times

1

Good morning, everyone.

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

Until then I can put everything right I bring the questions and mount the gridview with the radios button. However when saving the answers I need to pick the options dd Cad grid line 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 fill in 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 researches I made this code to check 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");
            }
        }
  • @bigown has some idea of another way that can be done or if this wrong this code?

  • I won’t touch it, but someone will come to help you soon.

  • I think it needs to be inserted into your gridview Onrowdatabound, so you can manipulate what’s inside it... Here is a guide explaining its function https://msdn.microsoft.com/pt-br/library/system.web.ui.webcontrols.gridview.onrowdatabound(v=vs.110). aspx

1 answer

0

Good morning.

I managed to solve my problem this way I don’t know if it is the best way but it works if someone wants to use or has another way

I added an action for 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 I can help someone and if someone has a different method of resolution.

att

Browser other questions tagged

You are not signed in. Login or sign up in order to post.