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 -
Here are more about GridView column types.
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...
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.