简体   繁体   English

GridView中的双列,从C#中的后面代码构建

[英]Double columns in GridView, building from code behind in C#

I'm trying to fill a GridView with a RadioButton and data from a CreditCardList. 我正在尝试使用RadioButton和CreditCardList中的数据填充GridView。 However, it's doubling the columns for each field... one full set of columns then another complete set (sans the RadioButton) I've checked creditCardList.items.Count and made sure it's just 1 (which it is). 但是,它会将每个字段的列加倍……一组完整的列,然后是另一组完整的集(没有RadioButton)。我已经检查了creditCardList.items.Count并确保它仅为1(是)。 What am I doing wrong? 我究竟做错了什么?

aspx: aspx:

<asp:GridView ID="gvCards" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
        <Columns>
            <asp:TemplateField HeaderText="Sel">
                <ItemTemplate>
                    <asp:RadioButton ID="Sel" runat="server" GroupName="rad" />
                </ItemTemplate>
            </asp:TemplateField>

            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Customer ID"  />
            <asp:BoundField DataField="Card ID" HeaderText="Card ID" />
            <asp:BoundField DataField="Card Number" HeaderText="Card Number" />
            <asp:BoundField DataField="Expiration" HeaderText="Expiration" />
            <asp:BoundField DataField="State" HeaderText="State" />
        </Columns>

    </asp:GridView>

Code behind: 后面的代码:

DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Customer ID", typeof(string));
dt.Columns.Add("Card ID", typeof(string));
dt.Columns.Add("Card Number", typeof(string));
dt.Columns.Add("Expiration", typeof(string));
dt.Columns.Add("State", typeof(string));
        for (var i = 0; i < creditCardList.items.Count; i++)
        {
            DataRow row1 = dt.NewRow();
            row1["Name"] = creditCardList.items[i].first_name + " " + creditCardList.items[i].last_name;
            row1["Customer ID"] = creditCardList.items[i].external_customer_id;
            row1["Card ID"] = creditCardList.items[i].id;
            row1["Card Number"] = creditCardList.items[i].number;
            row1["Expiration"] = creditCardList.items[i].expire_month + "/" + creditCardList.items[i].expire_year;
            row1["State"] = creditCardList.items[i].state;
            dt.Rows.Add(row1);
        }
        gvCards.DataSource = dt;
        gvCards.DataBind();

Output: Sel Name Card ID Card Number Expiration State Name Customer ID Card ID Card Number Expiration State Steve Ricketts %40LDN CON CARD-3F3 xxxxxxxxxxx1000 3/2020 ok Steve Ricketts %40LDN CON CARD-3F3 xxxxxxxxxxx1000 3/2020 ok 输出:Sel姓名卡ID卡号到期状态名称客户ID卡ID卡号到期状态Steve Ricketts%40LDN CON CARD-3F3 xxxxxxxxxxx1000 3/2020 ok Steve Ricketts%40LDN CON CARD-3F3 xxxxxxxxxxx1000 3/2020 ok

set AutoGenerateColumns="false" . 设置AutoGenerateColumns="false"

<asp:GridView ID="MyGrid" runat="server" AutoGenerateColumns="false">
     <Columns>
     </Columns>
</asp:GridView>

Remarks 备注
When the AutoGenerateColumns property is set to true, an AutoGeneratedField object is automatically created for each field in the data source. AutoGenerateColumns属性设置为true时,将自动为数据源中的每个字段创建一个AutoGeneratedField对象。 Each field is then displayed as a column in the GridView control in the order that the fields appear in the data source. 然后,每个字段在GridView控件中显示为一列,顺序是字段在数据源中出现的顺序。 This option provides a convenient way to display every field in the data source; 此选项提供了一种方便的方式来显示数据源中的每个字段。 however, you have limited control of how an automatically generated column field is displayed or behaves. 但是,您对自动显示的列字段的显示或行为控制有限。
Instead of letting the GridView control automatically generate the column fields, you can manually define the column fields by setting the AutoGenerateColumns property to false and then creating a custom Columns collection. 您可以通过将AutoGenerateColumns属性设置为false,然后创建一个自定义的Columns集合来手动定义列字段,而不是让GridView控件自动生成列字段。 In addition to bound column fields, you can also display a button column field, a check box column field, a command field, a hyperlink column field, an image field, or a column field based on your own custom-defined template. 除了绑定列字段之外,您还可以基于您自己的自定义模板显示按钮列字段,复选框列字段,命令字段,超链接列字段,图像字段或列字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM