C# - 填充 Gridview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17454910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:30:37  来源:igfitidea点击:

C# - Populating Gridview

c#asp.netviewgrid

提问by Matthew

I want to create a grid view with one column containing empty textboxes where the user can input a number (quantity), some regular columns and a column dedicated to images.

我想创建一个包含空文本框的列的网格视图,用户可以在其中输入一个数字(数量)、一些常规列和一个专用于图像的列。

I have the following code in C#:

我在 C# 中有以下代码:

    Label_Error.Visible = false;

    DataTable dt = new DataTable();
    dt.Columns.Add("Quantity", typeof(TextBox));
    dt.Columns.Add("Book ID", typeof(int));
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("Author", typeof(string));
    dt.Columns.Add("Description", typeof(string));
    dt.Columns.Add("Price", typeof(float));
    dt.Columns.Add("Currency", typeof(string));
    dt.Columns.Add("Image", typeof(string));

    DataRow row1 = dt.NewRow();
    row1["Quantity"] = new TextBox();
    row1["Book ID"] = 1;
    row1["Name"] = "Moby Dick";
    row1["Author"] = "Herman Melville";
    row1["Description"] = "Adventure Book";
    row1["Price"] = 10;
    row1["Currency"] = "EUR";
    row1["Image"] = ResolveUrl("~/Images/Logo.png");
    dt.Rows.Add(row1);

    GridView_Products.DataSource = dt;
    GridView_Products.DataBind();

This is what I am getting at the output:

这就是我在输出中得到的:

enter image description here

在此处输入图片说明

As you can see, the quantity column of empty textboxes is not being shown and the image is not being shown neither. How can I solve these two problems please?

如您所见,空文本框的数量列未显示,图像也未显示。请问如何解决这两个问题?

Update

更新

This is the code in the .aspx page:

这是 .aspx 页面中的代码:

<asp:GridView ID="GridView_Products" runat="server" BackColor="White" 
            BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
            HorizontalAlign="Center">
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" 
                HorizontalAlign="Center" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <RowStyle ForeColor="#000066" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#007DBB" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#00547E" />
        </asp:GridView>

采纳答案by Win

You need to create column by yourself instead of auto generating them.

您需要自己创建列而不是自动生成它们。

Here is an example -

这是一个例子——

enter image description here

在此处输入图片说明

<asp:GridView ID="GridView_Products" runat="server" BackColor="White"
    BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"
    HorizontalAlign="Center" AutoGenerateColumns="False">
    <FooterStyle BackColor="White" ForeColor="#000066" />
    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"
        HorizontalAlign="Center" />
    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
    <RowStyle ForeColor="#000066" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#007DBB" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#00547E" />
    <Columns>
        <asp:BoundField DataField="Book ID" HeaderText="Book ID" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Author" HeaderText="Author" />
        <asp:BoundField DataField="Description" HeaderText="Description" />
        <asp:BoundField DataField="Price" HeaderText="Price" />
        <asp:BoundField DataField="Currency" HeaderText="Currency" />
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:TextBox ID="QantityTextBox" runat="server" Text='<%# Eval("Quantity") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Image">
            <ItemTemplate>
                <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Image") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

DataTable dt = new DataTable();
dt.Columns.Add("Quantity", typeof(int)); // Make sure this is integer
dt.Columns.Add("Book ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Author", typeof(string));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("Price", typeof(float));
dt.Columns.Add("Currency", typeof(string));
dt.Columns.Add("Image", typeof(string));

DataRow row1 = dt.NewRow();
row1["Quantity"] = 1;
row1["Book ID"] = 1;
row1["Name"] = "Moby Dick";
row1["Author"] = "Herman Melville";
row1["Description"] = "Adventure Book";
row1["Price"] = 10;
row1["Currency"] = "EUR";
row1["Image"] = "~/Images/Logo.png";
dt.Rows.Add(row1);

GridView_Products.DataSource = dt;
GridView_Products.DataBind();

Here are more about GridViewcolumn types.

这里有更多关于GridView列类型的信息。

回答by snowYetis

Not knowing the requested specs, I would suggest using an EditItemTemplate on your GridView and placing a textbox control inside that.

不知道请求的规格,我建议在您的 GridView 上使用 EditItemTemplate 并在其中放置一个文本框控件。

Do you have to programatically create your gridview? How about this...

您是否必须以编程方式创建您的 gridview?这个怎么样...

<asp:gridview>
<columns>
<asp:TemplateField HeaderText="...">
<ItemTemplate>
<asp:Label ID="lbl_Quantity" runat="server" Text='<%# Eval("PutDateFieldNameHere") %>'
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Textbox ID="txt_Quantity" runat="server" Text='<%# Eval("DataFieldName") %>'></asp:Textbox>
</EditItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_Edit" runat="server" CommandName="EditQty" Text="Edit" CommandArgument='<%# ("ItemId") %>' />
</ItemTemplate>
</columns>
</asp:gridview>

This design requires a button to show the textbox, but there are other ways to implement this. If you go with the button approach make sure you wire up a CommandName event in your code behind and that the primary key for each item quantity you are changing is bound to the button.

这种设计需要一个按钮来显示文本框,但还有其他方法可以实现这一点。如果您使用按钮方法,请确保您在后面的代码中连接了一个 CommandName 事件,并且您正在更改的每个项目数量的主键都绑定到按钮。