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.
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.
Related
I would to add the selected rows from gridview into another gridview. Could someone help me on this? When I try to bind gridview to another gridview, another gridview doesn't appear.
The aspx file I posted below is about how I created the gridview. The gridview1 will get its gridview contents from the database table. While the cs file is about how I am going to bind selected rows from gridview1 into gridview2. The problem now is when I try to bind the rows over, the gridview2 didn't appear, only gridview1 appeared.
This is my codes:
ASPX file:
<h3 class="h3">Grid View1</h3>
<div style="width: 100%; height: 400px; overflow: auto">
<asp:GridView ID="GridView1"
runat="server"
AllowSorting="True"
AutoGenerateColumns="False"
Width="100%"
CellPadding="6"
ForeColor="#333333"
GridLines="Horizontal"
BorderColor="Black"
BorderStyle="Solid"
BorderWidth="2px"
EmptyDataText="Record Not Found"
OnRowDataBound="GridView1_OnRowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="DATE"
HeaderText="DATE"></asp:BoundField>
<asp:BoundField DataField="CODE"
HeaderText="CODE"></asp:BoundField>
<asp:BoundField DataField="PROFILE_NAME"
HeaderText="PROFILE_NAME"></asp:BoundField>
<asp:BoundField DataField="DESCRIPTION"
HeaderText="DESCRIPTION"></asp:BoundField>
<asp:BoundField DataField="STATUS"
HeaderText="STATUS"></asp:BoundField>
<asp:BoundField DataField="USER"
HeaderText="USER"></asp:BoundField>
<asp:BoundField DataField="SUB_USER"
HeaderText="SUB_USER"></asp:BoundField>
<asp:BoundField DataField="SCORE"
HeaderText="SCORE"></asp:BoundField>
<asp:BoundField DataField="ROLE"
HeaderText="ROLE"></asp:BoundField>
<asp:BoundField DataField="QUANTITY"
HeaderText="QUANTITY"></asp:BoundField>
<asp:BoundField DataField="ITEM"
HeaderText="ITEM"></asp:BoundField>
<asp:BoundField DataField="PRICE"
HeaderText="PRICE"></asp:BoundField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1"
Font-Bold="False"
ForeColor="Black" />
<HeaderStyle BackColor="#507CD1"
Font-Bold="False"
ForeColor="Black"
BorderStyle="Solid"
BorderWidth="2px" />
<PagerStyle BackColor="#2461BF"
ForeColor="White"
HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1"
Font-Bold="False"
ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<h3 class="h3">Grid View2</h3>
<div style="width: 100%; height: 400px; overflow: auto">
<asp:GridView ID="GridView2"
runat="server"
AllowSorting="True"
AutoGenerateColumns="False"
Width="100%"
CellPadding="6"
ForeColor="#333333"
GridLines="Horizontal"
BorderColor="Black"
BorderStyle="Solid"
BorderWidth="2px"
EmptyDataText="Record Not Found"
OnRowDataBound="GridView2_OnRowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="DATE"
HeaderText="DATE"></asp:BoundField>
<asp:BoundField DataField="CODE"
HeaderText="CODE"></asp:BoundField>
<asp:BoundField DataField="PROFILE_NAME"
HeaderText="PROFILE_NAME"></asp:BoundField>
<asp:BoundField DataField="DESCRIPTION"
HeaderText="DESCRIPTION"></asp:BoundField>
<asp:BoundField DataField="STATUS"
HeaderText="STATUS"></asp:BoundField>
<asp:BoundField DataField="USER"
HeaderText="USER"></asp:BoundField>
<asp:BoundField DataField="SUB_USER"
HeaderText="SUB_USER"></asp:BoundField>
<asp:BoundField DataField="SCORE"
HeaderText="SCORE"></asp:BoundField>
<asp:BoundField DataField="ROLE"
HeaderText="ROLE"></asp:BoundField>
<asp:BoundField DataField="QUANTITY"
HeaderText="QUANTITY"></asp:BoundField>
<asp:BoundField DataField="ITEM"
HeaderText="ITEM"></asp:BoundField>
<asp:BoundField DataField="PRICE"
HeaderText="PRICE"></asp:BoundField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1"
Font-Bold="False"
ForeColor="Black" />
<HeaderStyle BackColor="#507CD1"
Font-Bold="False"
ForeColor="Black"
BorderStyle="Solid"
BorderWidth="2px" />
<PagerStyle BackColor="#2461BF"
ForeColor="White"
HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1"
Font-Bold="False"
ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
CS file:
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, “USER”)) == “ALAN TAN”)
{
DataTable dt = new DataTable();
dt.Columns.Add(“DATE”);
dt.Columns.Add(“CODE”);
dt.Columns.Add(“PROFILE_NAME”);
dt.Columns.Add(“DESCRIPTION”);
dt.Columns.Add(“STATUS”);
dt.Columns.Add(“USER”);
dt.Columns.Add(“SUB_USER”);
dt.Columns.Add(“SCORE”);
dt.Columns.Add(“ROLE”);
dt.Columns.Add(“QUANTITY”);
dt.Columns.Add(“ITEM”);
dt.Columns.Add(“PRICE”);
DataRow dataRow;
dataRow = dt.NewRow();
int i2 = 1;
for(int i=0; i<dataRow.Table.Columns.Count; i++)
{
dataRow[i] = GridView1.SelectedRow.Cells[i2].Text;
i2++;
}
dt.Rows.Add(dataRow);
GridView2.DataSource = dt;
GridView2.DataBind();
}
}
}
Any help is greatly appreciated. Thanks!
I Really didn't understand messy code done by you (Too many things are missing in your question). Refer below links that will explain you will example.
http://www.aspsnippets.com/Articles/Transfer-Selected-Rows-from-one-GridView-to-Another-in-Asp.net.aspx
http://www.ittutorials.in/source/aspDotnet/scf13/move-gridview-rows-from-one-gridview-to-another.aspx
Updates:
dt.Rows.Add() must be inside of for loop so everytime it will get add new row.
try to update following code in cs file
for(int i=0; i<dataRow.Table.Columns.Count; i++)
{
dt.Rows.Add(GridView1.SelectedRow.Cells[i2].Text);
i2++;
}
GridView2.DataSource = dt;
GridView2.DataBind();
I'm using the below code to add a new row to a ASP.net gridview. The problem is that every new row I create is blank. I looped through the cells in the row and found that most of them were spaces ( ). I've looked everywhere for a different piece of code to use, but everything leads me to: create a new datatable, add a row to it, set the gridview datasource to the datatable. I've used this control a dozen times before and never got this before, it's very odd.
Class Level Variable
public partial class DMREntry : System.Web.UI.Page
{
private DataTable _Parts
{
get { return (DataTable)ViewState["Parts"]; }
set { ViewState.Add("Parts", value); }
}
...
The add row code. Note that my textboxes and my gridview are within an update panel.
if (_Parts == null)
{
_Parts = new DataTable();
_Parts.Columns.Add("Part No");
_Parts.Columns.Add("Qty");
_Parts.Columns.Add("Description");
_Parts.Columns.Add("Vendor");
_Parts.Columns.Add("Vendor Part");
_Parts.Columns.Add("Cost");
_Parts.Columns.Add("PO Number");
_Parts.Columns.Add("Delivery Date");
_Parts.Columns.Add("Total Cost");
}
DataRow dr = _Parts.NewRow();
dr["Part No"] = txtPartNo.Text;
dr["Qty"] = txtQty.Text;
dr["Description"] = txtDescription.Text;
dr["Vendor"] = txtVendor.Text;
dr["Vendor Part"] = txtVendorPart.Text;
dr["Cost"] = txtCost.Text;
dr["PO Number"] = txtPONumber.Text;
dr["Delivery Date"] = txtDeliveryDate.Text;
dr["Total Cost"] = txtTotalCost.Text;
// At this point, DR has the correct values
_Parts.Rows.Add(dr);
_Parts.AcceptChanges();
gvParts.DataSource = _Parts;
gvParts.DataBind();
upGrid.Update();
GridView markup:
`<asp:GridView ID="gvParts" runat="server" AutoGenerateColumns="False" PageSize="5"
Width="787px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="3"
onselectedindexchanged="gvParts_SelectedIndexChanged1" AllowPaging="True"
AllowSorting="True">
<Columns>
<asp:TemplateField HeaderText="Part No"></asp:TemplateField>
<asp:TemplateField HeaderText="Qty"></asp:TemplateField>
<asp:TemplateField HeaderText="Description"></asp:TemplateField>
<asp:TemplateField HeaderText="Vendor"></asp:TemplateField>
<asp:TemplateField HeaderText="Vendor Part"></asp:TemplateField>
<asp:TemplateField HeaderText="Cost"></asp:TemplateField>
<asp:TemplateField HeaderText="PO Number"></asp:TemplateField>
<asp:TemplateField HeaderText="Delivery Date"></asp:TemplateField>
<asp:TemplateField HeaderText="Total Cost"></asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowEditButton="True" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>`
Leaves me with:
My grid properties are the default, except:
AutoGenerateColumns = false
AllowPaging = true
AllowSorting = true
PageSize = 5
If I turn AutoGenerateColumns on, the data from the table does go into the auto-generated columns. But not for when I create the columns manually. I am using the template field column type for the manually added columns. They're just text fields, I couldn't find a better option.
Any help appreciated...
Why you are using asp:TemplateField ? You did not add any control in the templatefield.
Just change TemplateField to BoundField then it will solve your problem.
Like:
<asp:BoundField DataField="Part No" HeaderText="Part No" />
<asp:BoundField DataField="Qty" HeaderText="Qty" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="Vendor" HeaderText="Vendor" />
<asp:BoundField DataField="Vendor Part" HeaderText="Vendor Part" />
<asp:BoundField DataField="Cost" HeaderText="Cost" />
<asp:BoundField DataField="PO Number" HeaderText="PO Number" />
<asp:BoundField DataField="Delivery Date" HeaderText="Delivery Date" />
<asp:BoundField DataField="Total Cost" HeaderText="Total Cost" />
Try creating inside the an tag
<asp:TemplateField HeaderText="Example">
<ItemTemplate>
<%# Eval("TestColumn") %>
</ItemTemplate>
</asp:TemplateField>
I would like to manually add rows to a GridView, and display it at Page Load. For some reason, my current code shows an empty GridView.
Default.aspx
<asp:GridView ID="AllocationGridView" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False"
Width="691px" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True"
AutoPostBack="true">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" Height="2px" />
<Columns>
<asp:TemplateField HeaderText="Asset Class">
<ItemStyle Font-Size="13px" Width="20%" />
<ItemTemplate>
<asp:Label ID="AssetLabel" runat="server" ReadOnly="true" Text="" BorderWidth="0px"
Style="text-align: left;"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weight">
<ItemStyle Font-Size="13px" Width="20%" />
<ItemTemplate>
<asp:TextBox ID="WeightTextBox" runat="server" ReadOnly="true" BorderWidth="0px"
Style="text-align: left;"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#EBEBEB" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" Height="10px" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FirstAllocationGridViewRow();
}
}
protected void FirstAllocationGridViewRow()
{
DataTable table = new DataTable();
string[] assets = new string[] { "Cash", "US Equity", "Fixed Income", "BAS", "International" };
table.Columns.Add(new DataColumn("Col1", typeof(string)));
table.Columns.Add(new DataColumn("Col2", typeof(double)));
DataRow dr = table.NewRow();
for (int i = 0; i < assets.Count(); i++)
{
dr = table.NewRow();
dr["Col1"] = assets[i];
dr["Col2"] = DBNull.Value;
table.Rows.Add(dr);
}
ViewState["currentAllocationTable"] = table;
AllocationGridView.Visible = true;
AllocationGridView.DataSource = table;
AllocationGridView.DataBind();
}
your cs code is fine the problem is in your layout code. text property is not bind to table field name
Text='<%# Bind("Col1") %>'
here is the complete gridview
<asp:GridView ID="AllocationGridView" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False"
Width="691px" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True"
AutoPostBack="true">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" Height="2px" />
<Columns>
<asp:TemplateField HeaderText="Asset Class">
<ItemStyle Font-Size="13px" Width="20%" />
<ItemTemplate>
<asp:Label ID="AssetLabel" runat="server" ReadOnly="true" Text='<%# Bind("Col1") %>' BorderWidth="0px"
Style="text-align: left;"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weight">
<ItemStyle Font-Size="13px" Width="20%" />
<ItemTemplate>
<asp:TextBox ID="WeightTextBox" runat="server" Text='<%# Bind("Col2") %>' ReadOnly="true" BorderWidth="0px"
Style="text-align: left;"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#EBEBEB" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" Height="10px" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Remove the AutoPostBack="true" field in Gridview control. And make sure the Data-table have correct values for the columns and please see Text="" in your gridview labels . You need give values for the labels.
See this link , It's may helps you
Your problem is with your Template Fields as you cannot set values to them using a DataTable, change them to BoundFields or set AutoGenerateColumns="True"
Tried your code with my answer, it worked fine.
I am creating a DataTable and populating the rows/columns in it.
However when I bind it to a GridView, I cannot see the text I assign to one column.
Code:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
DataRow dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = "some text";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["RowNumber"] = 2;
dr["Column1"] = "more text";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["RowNumber"] = 3;
dr["Column1"] = "and more text";
dt.Rows.Add(dr);
Gridview2.DataSource = dt;
Gridview2.DataBind();
The gridview only displays the first Column (with the numbers 1-3) but the 2nd column with the text does not display on the gridview.
Any ideas?
Thanks!
EDIT: The 1st column gridview is in BoundField whereas the 2nd column is within TemplateFied...ItemTemplate.
<asp:gridview ID="Gridview2" runat="server" ShowFooter="true" GridLines="None"
AutoGenerateColumns="false" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="#" ItemStyle-Width="30px" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Width="300"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd1" runat="server" Text="Add Statement" onclick="ButtonAdd1_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
You have not used the second column in your gridview.
Either use another bound field as follows
<asp:BoundField DataField="Column1" HeaderText="#" ItemStyle-Width="30px" />
Or Modify your template filed as follows
<ItemTemplate>
<asp:TextBox ID="TextBox1" Text='<%# Eval("Column1") %>' runat="server" Width="300">
</asp:TextBox>
</ItemTemplate>
Edit 1
Your structure of the gridview is strange as you have
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd1" runat="server" Text="Add Statement"
onclick="ButtonAdd1_Click" />
</FooterTemplate>
inside <asp:TemplateField>.
You can do like this the Markup. Use label/textbox based on your requirement
<asp:TemplateField HeaderText="Column1">
<ItemTemplate>
<asp:Label ID="lblColumn1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Column1")%'></asp:Label>
</ItemTemplate> </asp:TemplateField>
I have a GridView that is built from a Linq query as follows
var GridViewLoad = from d in QVuser.QlikViewDashboards.AsEnumerable()
join p in tempPermissions.AsEnumerable() on d.DashboardId equals Convert.ToInt32(p["DashboardId"])
where Convert.ToInt32(p["UserId"]) == GridViewUser
select new
{
DashboardId = d.DashboardId,
PermissionId = Convert.ToInt32(p["PermissionId"]),
DashboardName = d.DashboardName,
Operational_Unit = p["Operational_Unit"].ToString(),
Cost_Centre = p["Cost_Centre"].ToString(),
Project = p["Project"].ToString(),
Fund = p["Fund"].ToString()
};
GridView1.DataSource = GridViewLoad;
GridView1.DataKeyNames = new string[] {"PermissionId"};
GridView1.DataBind();
Then the GridView is defined as:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3"
GridLines="Vertical" ShowHeaderWhenEmpty="True" OnRowDeleting="GridView1_RowDeleting"
style="text-align: center" BackColor="White" BorderColor="#999999"
BorderStyle="None" BorderWidth="1px" Width="550px"
>
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField HeaderText="Delete" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="false"
CommandArgument='<%# Eval("PermissionId") %>' CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PermissionId" HeaderText="ID"/>
<asp:BoundField DataField="DashboardName" HeaderText="Dashboard" ReadOnly="True" SortExpression="DashboardName" />
<asp:BoundField DataField="Operational_Unit" HeaderText="Operational_Unit" ReadOnly="True" SortExpression="Operational_Unit" />
<asp:BoundField DataField="Cost_Centre" HeaderText="Cost_Centre" ReadOnly="True" SortExpression="Cost_Centre" />
<asp:BoundField DataField="Fund" HeaderText="Fund" ReadOnly="True" SortExpression="Fund" />
<asp:BoundField DataField="Project" HeaderText="Project" ReadOnly="True" SortExpression="Project" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" BorderStyle="Solid" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" BorderStyle="Solid"
BorderWidth="1px" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
What I want to do is hide the PermissionId field from sight, but it obviously still needs to be there for the Delete button to work..
Can someone help me out with this?
I've tried setting Visible="false", which hides it, but then my delete button stops working..
Cheers for your help..
Since you're setting the DataKeyNames, you should be able to retrieve the permission ID from the index of the row being deleted, like so:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int permissionId = (int)GridView1.DataKeys[e.RowIndex].Value;
DoDelete(permissionId);
}
This should work whether the column is visible or not.
Try
<asp:BoundField DataField="PermissionId" HeaderText="ID" Visible="False"/>