C# - 填充GridView [英] C# - Populating Gridview

查看:112
本文介绍了C# - 填充GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我在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] =赫尔曼梅尔维尔;
row1 [Description] =冒险书;
row1 [Price] = 10;
row1 [Currency] =EUR;
row1 [Image] = ResolveUrl(〜/ Images / Logo.png);
dt.Rows.Add(row1);

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

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





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

更新

这是在.aspx页面中的代码:

 < asp:GridView ID =GridView_Productsrunat =serverBackColor =White 
BorderColor =#CCCCCCBorderStyle =NoneBorderWidth =1pxCellPadding =3
Horizo​​ntalAlign =Center>
< FooterStyle BackColor =WhiteForeColor =#000066/>
< HeaderStyle BackColor =#006699Font-Bold =TrueForeColor =White
Horizo​​ntalAlign =Center/>
< RowStyle ForeColor =#000066Horizo​​ntalAlign =Center/>
< SelectedRowStyle BackColor =#669999Font-Bold =TrueForeColor =White/>
< SortedAscendingCellStyle BackColor =#F1F1F1/>
< SortedAscendingHeaderStyle BackColor =#007DBB/>
< SortedDescendingCellStyle BackColor =#CAC9C9/>
< SortedDescendingHeaderStyle BackColor =#00547E/>
< / asp:GridView>


解决方案

您需要自行创建列而不是自动生成他们。



以下是一个例子 -




 < asp:GridView ID =GridView_Products RUNAT = 服务器背景色= 白 
BORDERCOLOR = #CCCCCC 边框= 无 边框宽度= 1px的 CELLPADDING = 3
Horizo​​ntalAlign = 中心 的AutoGenerateColumns = 假 >
< FooterStyle BackColor =WhiteForeColor =#000066/>
< HeaderStyle BackColor =#006699Font-Bold =TrueForeColor =White
Horizo​​ntalAlign =Center/>
< RowStyle ForeColor =#000066Horizo​​ntalAlign =Center/>
< SelectedRowStyle BackColor =#669999Font-Bold =TrueForeColor =White/>
< SortedAscendingCellStyle BackColor =#F1F1F1/>
< SortedAscendingHeaderStyle BackColor =#007DBB/>
< SortedDescendingCellStyle BackColor =#CAC9C9/>
< SortedDescendingHeaderStyle BackColor =#00547E/>
<列>
< asp:BoundField DataField =Book IDHeaderText =Book ID/>
< asp:BoundField DataField =NameHeaderText =Name/>
< asp:BoundField DataField =AuthorHeaderText =Author/>
< asp:BoundField DataField =DescriptionHeaderText =Description/>
< asp:BoundField DataField =PriceHeaderText =Price/>
< asp:BoundField DataField =CurrencyHeaderText =Currency/>
< asp:TemplateField HeaderText =Quantity>
< ItemTemplate>
< asp:TextBox ID =QantityTextBoxrunat =serverText ='<%#Eval(Quantity)%>'/>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =图片>
< ItemTemplate>
< asp:Image ID =Image1runat =serverImageUrl ='<%#Eval(Image)%>'/>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>

DataTable dt = new DataTable();
dt.Columns.Add(Quantity,typeof(int)); //确定这是整数
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] =赫尔曼梅尔维尔;
row1 [Description] =冒险书;
row1 [Price] = 10;
row1 [Currency] =EUR;
row1 [Image] =〜/ Images / Logo.png;
dt.Rows.Add(row1);

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

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


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#:

    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:

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:

<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>

解决方案

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

Here is an example -

<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 GridView column types.

这篇关于C# - 填充GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆