C# - Populating Gridview

2019-08-09 05:23发布

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:

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:

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

2条回答
三岁会撩人
2楼-- · 2019-08-09 05:47

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

查看更多
Lonely孤独者°
3楼-- · 2019-08-09 06:01

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

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

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

查看更多
登录 后发表回答