Gridview empty rows issue - c#

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>

Related

How to get previous value before editing from grid view text box in asp.net

I'm applying edit functionality on grid view with object data source.I want to take previous value when i click edit button that's need for another purpose here i'm trying to write code but it gives current value.how to get previous value.......hope you can help me
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridView1.Rows[e.RowIndex] as GridViewRow);
string totalQuantity = (row.Cells[5].Controls[0] as TextBox).Text;//It gives current value but i want previous value
string totalPurchasePrice = (row.Cells[6].Controls[0] as TextBox).Text; //It gives current value but i want previous value
GridView1.EditIndex = -1;
//value store in session start
Session["totalQuantity"] = totalQuantity;
Session["totalPurchasePrice"] = totalPurchasePrice;
//value store in session end
}
and grid view code
<asp:GridView ID="GridView1" runat="server" CssClass="dataGridTable" AutoGenerateColumns="False" Width="100%" AllowPaging="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="ObjectDataSource1" HeaderStyle-Height="30" OnRowCreated="GridView1_RowCreated" PageSize="30" DataKeyNames="invoiceNumber,productName,productCategory" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="true" ShowSelectButton="True" />
<asp:CommandField ShowDeleteButton="true"/>
<asp:BoundField DataField="invoiceNumber" HeaderText="Invoice" ReadOnly="true" SortExpression="invoiceNumber" />
<asp:BoundField DataField="productName" HeaderText="Name" ReadOnly="true" SortExpression="productName" />
<asp:BoundField DataField="productCategory" HeaderText="Category" ReadOnly="true" SortExpression="productCategory" />
<asp:BoundField DataField="totalQuantity" HeaderText="Quantity" SortExpression="totalQuantity" />
<asp:BoundField DataField="totalPurchasePrice" HeaderText="Total Price" SortExpression="totalPurchasePrice" />
<asp:BoundField DataField="salePricePerItem" HeaderText="Sale Price/Item" SortExpression="salePricePerItem" />
<asp:BoundField DataField="comments" HeaderText="Comments" SortExpression="comments" />
<asp:BoundField DataField="date" HeaderText="Date" ReadOnly="true" SortExpression="date" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" Height="24px" ImageUrl="~/Images/detailsInfo.png" Width="24px" OnClick="ImageButton1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" CssClass="gridHeaderAlignment" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>

How to Bind Selected Rows From Grid View to Another Grid View

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();

Gridview Command and Selected Row

I am trying to get the selected row data and transfer it to a label on the same page. However I cannot get the Gridview.SelectedRow to work with my CommandName. I have tried everything....
I get the error. Object reference not set to an instance of an object. on Label2.Text
Here is my Code:
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple ButtonField column fields are used, use the
// CommandName property to determine which button was clicked.
if (e.CommandName == "Grab")
{
Label2.Text = GridView1.SelectedRow.Cells[2].Text;
Label3.Text = GridView1.SelectedRow.Cells[3].Text;
Label4.Text = GridView1.SelectedRow.Cells[4].Text;
Label5.Text = GridView1.SelectedRow.Cells[5].Text;
}
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" CssClass="td" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="574px" onrowcommand="GridView1_RowCommand">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/edit.png" OnClick="ImageButton2_Click" />
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="Select" ImageUrl="~/images/delete.png" onclientclick=" return confirm('Are you want to Delete this Vehicle?');" />
<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/images/refre.png" CommandName="Grab" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />
<asp:BoundField DataField="Make" HeaderText="Make" SortExpression="Make" />
<asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
<asp:BoundField DataField="Submodel" HeaderText="Submodel" SortExpression="Submodel" />
<asp:BoundField DataField="ISENABLED" HeaderText="ISENABLED" SortExpression="ISENABLED" />
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
Try something like this. Since you're trying to access the values in the command event and not in the OnSelectedIndexChanged event you need to get hold of the row triggering the command event first.
if (e.CommandName == "Grab")
{
GridViewRow row = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
if (row != null)
{
Label2.Text = row.Cells[2].Text;
Label3.Text = row.Cells[3].Text;
Label4.Text = row.Cells[4].Text;
Label5.Text = row.Cells[5].Text;
}
}

Displaying CheckBox in Gridview DataBound based on varchar column

SQL Server table structure:
ChapName varchar(200)
Status varchar(1)
Requirement:
I want to display checkbox in an ASP.NET gridview from Visual Studio 2010
if the value of status column is T, let it be checked and unchecked otherwise.
but it shows only textbox.
I have tried <asp:templatefield> and <asp:itemtemplate> but it throws error if I try to bind this checkbox.
any sample code is required as I am beginner in this field.
The code I tried:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox c = (CheckBox)GridView1.FindControl("ChkStatus");
TextBox TB = (TextBox)GridView1.FindControl("Status");
//Response.Write(TB.Text);
if (TB.Text == "T")
{
c.Checked = true;
}
else
{
c.Checked = false;
}
}
The error I got
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the
error and where it originated in the code.
Exception Details:
System.NullReferenceException: Object reference not set to an instance
of an object.
Aspx markup:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Comp,QTypeCode" DataSourceID="SDS_QType_Edit"
BackColor="White" BorderColor="#DEDFDE" BorderStyle="None"
BorderWidth="1px"
CellPadding="4" ForeColor="Black" GridLines="Vertical"
AllowPaging="True" AllowSorting="True"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowdatabound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="QTypeCode" HeaderText="QTypeCode"
SortExpression="QTypeCode" InsertVisible="False"
ReadOnly="True" />
<asp:BoundField DataField="Descr" HeaderText="Descr" SortExpression="Descr" />
<asp:CheckBoxField DataField="AnsReq" HeaderText="AnsReq" ReadOnly="True"
SortExpression="AnsReq" />
<asp:CheckBoxField DataField="OptionPrint" HeaderText="OptionPrint"
ReadOnly="True" SortExpression="OptionPrint" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
<asp:BoundField DataField="TransDate" HeaderText="TransDate"
SortExpression="TransDate" />
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" />
<asp:TemplateField HeaderText="Check Box" >
<ItemTemplate>
<asp:CheckBox ID ="ChkStatus" Text="text" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle Wrap="False" />
<EmptyDataRowStyle Wrap="False" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle Wrap="False" BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle Wrap="False" BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White"
Wrap="False" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
Assume you have grid defined as below on asmx
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ChapName" HeaderText="ChapName" />
<asp:TemplateField HeaderText="Status" Visible ="false">
<ItemTemplate>
<asp:Label ID="Status" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Check Box" >
<ItemTemplate>
<asp:CheckBox ID ="ChkStatus" Text="text" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
on Row Data Bound event you can find controls as below
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox c = e.Row.FindControl("ChkStatus") as CheckBox;
Label lbl = e.Row.FindControl("Status") as Label;
if (c!= null && lbl != null )
{
c.Checked = (lbl.Text == "T");
}
}

How do I hide my primary key from GridView but still reference it

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

Categories

Resources