-1

请任何人都可以建议一种将数据库表数据绑定到gridview的方法,但是在特定的布局中,列值应该出现在主行的子行中,就像数据库表中的所有行都应该在那个Gridview中列出一样。我知道将数据绑定到gridview的常规方式将行数据(来自数据库表)显示为列,但我想要的如下所示 在此处输入图像描述

4

1 回答 1

0

Gridview is not suitable for this type of format.

If you are okay with repeater, you can do something like this:

And here's a link to MSDN: Repeater Class.

UPDATE: If you want to post each answer selection, you can use option button and group them. You can use questionid as a part of group name and in the code get the question id. Your markup may look like below:

<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <table border="1">
            <tr>
                <th colspan="2"></th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><%# DataBinder.Eval(Container.DataItem, "QuestionId") %> </td>
            <td>
                <table>
                    <tr>
                        <td colspan="4"><%# DataBinder.Eval(Container.DataItem, "Question") %> </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer1") %>' 
                                OnCheckedChanged="RadioButton1_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />                       
                        </td>
                        <td><asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer2") %>'  
                            OnCheckedChanged="RadioButton2_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />                        
                        </td>
                        <td><asp:RadioButton ID="RadioButton3" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer3") %>'  
                            OnCheckedChanged="RadioButton3_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
                        </td>
                        <td><asp:RadioButton ID="RadioButton4" runat="server" AutoPostBack="true"  Text='<%# DataBinder.Eval(Container.DataItem, "Answer4") %>'  
                            OnCheckedChanged="RadioButton4_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

And in the code:

protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
    string grpId = ((RadioButton)sender).GroupName;
    int questionId = 0;

    int.TryParse(grpId.Split('_')[1].ToString(), out questionId);

    //Use questionId
}
于 2013-09-14T15:07:26.373 回答