I have radio button list in a gridview that needs to be bound to a column. If the value in a column is 0, the first radio button is selected, if 1, the other is selected.
This is the code, some of it is partially removed because it is not necessary
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<>"
SelectCommand="" SelectCommandType="StoredProcedure" UpdateCommand="">
<SelectParameters></SelectParameters>
<UpdateParameters></UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="gvBlockDetail" runat="server" AutoGenerateColumns="False"
DataKeyNames="curriculumyear,electiveid,blockid" DataSourceID="SqlDataSource1"
HorizontalAlign="Left" CellPadding="1" CssClass="news" GridLines="None"
BorderColor="#ebe9e2" BorderStyle="Solid" BorderWidth="1" >
<AlternatingRowStyle BackColor="#ebe9e2" />
<HeaderStyle BackColor="#660000" ForeColor="White" Font-Size="Small" />
<RowStyle Font-Size="9pt" Wrap="false" ForeColor="#660000" HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderText="Add/Remove">
<HeaderStyle Width="15%" />
<ItemStyle Wrap="false" Width="80px" />
<ItemTemplate>
<asp:RadioButtonList ID="rblAddRemove" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="Add" Value="0"></asp:ListItem>
<asp:ListItem Text="Remove" Value="1"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Block">
<HeaderStyle Width="15%" />
<ItemStyle Wrap="false" Width="50px" />
<ItemTemplate>
<asp:Label ID="lblBlock" runat="server" Text='<%# Bind("Block") %>'></asp:Label>
<asp:Label ID="lblSection" runat="server" Text='<%# Bind("Section") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="daterange" HeaderText="Dates" ReadOnly="True" SortExpression="daterange" />
<asp:BoundField DataField="credithours" HeaderText="Credit Hrs"
SortExpression="credithours" HeaderStyle-Width="10%" ItemStyle-Width="10%" />
<asp:TemplateField HeaderText="Students<br>Per Block" HeaderStyle-Width="15%" SortExpression="studentsperblock">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("studentsperblock") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtStudentsPerBlock" runat="server" MaxLength="3" Width="40px" Text='<%# Bind("studentsperblock") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="enrolled" HeaderText="Enrolled" ReadOnly="True"
SortExpression="enrolled" ItemStyle-Width="200px" />
<asp:BoundField DataField="blockid" HeaderText="blockid" ReadOnly="True"
SortExpression="blockid" Visible="false" />
</Columns>
</asp:GridView>
Codebehind:
Protected Sub gvBlockDetail_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBlockDetail.RowDataBound
End Sub
You could try inline binding:
<asp:RadioButtonList ID="rblAddRemove" runat="server" RepeatDirection="Horizontal" SelectedValue='<%# Bind("YOURCOLUMN") %>'>
<asp:ListItem Text="Add" Value="0"></asp:ListItem>
<asp:ListItem Text="Remove" Value="1"></asp:ListItem>
</asp:RadioButtonList>
Where yourcolumn is the int column you described.
Or via the RowDataBound event. (Pseudocode, the properties might have a different name and I'm using C#)
Protected Sub gvBlockDetail_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBlockDetail.RowDataBound
if(e.Row.RowType == RowType.DataRow)
{
RadioButtonList rbl = e.Row.FindControl("rblAddRemove") as RadioButtonList;
if(rbl != null)
{
rbl.SelectedValue = ((YOURDATAITEM)(e.Row.DataItem).YourProperty.ToString();
}
}
End Sub
Edit: I see you aren't using custom classes. You need to adjust the line with YOURDATAITEM. Use quick watch to get to know how to cast the object to get ahold of the desired property.
Related
I have a page with two gridview; one is inside the modal pop up (grdEmpName) and the other is in the page (gvUser). Now, when the user select search button the modal pop up extender will pop up and then search employees. When the user select someone, the selected data should display to the textbox in the another gridview (gvUser).
Everything is working up to selection in modal pop up extender gridview (grdEmpName)but when I select no data pass to the other gridview. (gvUser). I need to display the selected data in the textbox.
ASPX:
<asp:GridView ID="gvUser" runat="server" Width="1024px"
PageSize="10"
AutoGenerateColumns = "False"
AllowSorting="true"
AllowPaging="true"
ShowFooter="True"
OnSorting="gvUser_Sorting"
OnPageIndexChanging="gvUser_PageIndexChanging"
OnRowCreated="gvUser_RowCreated"
CssClass="Grid"
AlternatingRowStyle-CssClass="alt"
PagerStyle-CssClass="pgr"
FooterStyle-HorizontalAlign="center" >
<PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageImageUrl="~/images/gridview/first_16x16.png" LastPageImageUrl="~/images/gridview/last_16x16.png" />
<Columns>
<asp:TemplateField>
<ItemTemplate >
<asp:ImageButton ID="ibtnEdit" runat="server" ImageUrl="~/images/gridview/file_edit_24x24.png" title = "Edit" _CommandArgument='<%# Eval("USR_ID") & "|Edit|" & CType(Container,GridViewRow).RowIndex & "|-" %>' />
<asp:ImageButton ID="ibtnUpdate" runat="server" ImageUrl="~/images/gridview/file_save_24x24.png" visible="false" title="Update" _CommandArgument='<%# Eval("USR_ID") & "|Update|" & CType(Container,GridViewRow).RowIndex & "|-" %>' />
<asp:ImageButton ID="ibtnCancel" runat="server" ImageUrl="~/images/gridview/file_cancel_24x24.png" Visible="false" title="Cancel" _CommandArgument='<%# Eval("USR_ID")& "|Cancel|" & CType(Container,GridViewRow).RowIndex & "|-" %>' />
<asp:ImageButton ID="ibtnDelete" runat="server" ImageUrl="~/images/gridview/file_delete_24x24.png" title="Delete" _CommandArgument='<%# Eval("USR_ID")& "|Delete|" & CType(Container,GridViewRow).RowIndex & "|" & Eval("USR_IDNTY") %>' />
<asp:ImageButton ID="btnSearchUser" runat="server" ImageUrl="~/images/gridview/file_search_24x24.png" title="Search" _CommandArgument= "btnSearchUser" OnClick = "btnSearchUser_Click" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="ibtnAdd" runat="server" ImageUrl="~/images/gridview/file_add_24x24.png" title="Add" _CommandArgument='<%# Eval("USR_ID") & "|Add|" & CType(Container,GridViewRow).RowIndex & "|-" %>' />
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="User Identity" visible ="false" >
<ItemTemplate>
<asp:Label ID="lblUSR_IDNTY" runat="server" Text='<%# Bind("USR_IDNTY")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblUSR_IDNTY" runat="server" Width="95%" Text='<%# Bind("USR_IDNTY")%>' />
</EditItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="User ID" SortExpression="USR_ID">
<ItemTemplate>
<asp:Label ID="lblUSR_ID" runat="server" Text='<%# Bind("USR_ID")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtUSR_ID" runat="server" Width="95%" Text='<%# Bind("USR_ID")%>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtUSR_ID" runat="server" Width="95%"/>
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="NAME">
<ItemTemplate>
<asp:Label ID="lblNAME" runat="server" Text='<%# Bind("NAME")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNAME" runat="server" Width="95%" Text='<%# Bind("NAME")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNAME" runat="server" Width="95%"/>
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="EMAIL">
<ItemTemplate>
<asp:Label ID="lblEMAIL" runat="server" Text='<%# Bind("EMAIL")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEMAIL" runat="server" Width="95%" Text='<%# Bind("EMAIL")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEMAIL" runat="server" Width="95%"/>
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Active" SortExpression="ACTIVE">
<ItemTemplate>
<asp:CheckBox ID="chkACTIVE" runat="server" Checked ='<%# Bind("ACTIVE")%>' Enabled="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkACTIVE" runat="server" Width="95%" Checked ='<%# Bind("ACTIVE")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:CheckBox ID="chkACTIVE" runat="server" Width="95%"/>
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle HorizontalAlign="Center" Wrap="False"/>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<div style="height:315px;border-style:hidden;" >
<table class="table" >
<tr>
<td style="padding:5px;" >
<img id="Img1" style="height:24px;width:24px; " runat="server" src="images/no_data_64x64.png" />
</td>
<td style="padding:5px;font-size:large;">
<b>No data found.</b>
</td>
</tr>
</table>
</div>
</EmptyDataTemplate>
<PagerStyle CssClass="pgr" />
<AlternatingRowStyle CssClass="alt" />
<FooterStyle HorizontalAlign="Center" />
<EmptyDataRowStyle CssClass="EmptyData" />
</asp:GridView>
</div>
<%-- Picker--%>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<cc1:ModalPopupExtender runat="server" TargetControlID="hdControl" ID="mdlEmpListProcess" PopupControlID="empPanel" CancelControlID="btnClose" OnCancelScript="getFlickerSolved();" >
</cc1:ModalPopupExtender>
<asp:HiddenField runat="server" ID="hdControl" />
<asp:Panel runat="server" ID="empPanel" Width="909px" Height="300" BorderStyle="Ridge" BorderColor="Gray" Style="display:none;" BackColor="Control">
<table>
<tr>
<td>
<asp:TextBox runat="server" id="txtEmpName" CssClass="textbox" Width="240"></asp:TextBox>
<asp:Button runat="server" ID="btnSearchEmp" CssClass="button" Text="SEARCH" OnClientClick="getFlickerSolved();"/>
<asp:Button runat="server" ID="btnClose" CssClass="button" Text="CLOSE" />
</td>
</tr>
</table> <br />
<div style="overflow:scroll; width:882px; height:233px;">
<asp:GridView runat="server" ID="grdEmpName" Width="863px" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" Height="35px" GridLines="Vertical">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton runat="server" ID="optEmp" AutoPostBack="true" _CommandArgument= "Select" OnCheckedChanged="selectedEmployee" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="USR_ID" HeaderText="USER ID"/>
<asp:BoundField DataField="NAME" HeaderText="NAME"/>
<asp:BoundField DataField="EMAIL" HeaderText="EMAIL ADDRESS">
<HeaderStyle Width="300px" />
</asp:BoundField>
</Columns>
<RowStyle CssClass="rowStyle" />
<HeaderStyle CssClass="headerStyle" BackColor="Black" Font-Bold="True" ForeColor="White" />
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
</div>
</asp:Panel>
<%-- End Picker--%>
Now, here's may code for selectedEmployee (Radio Button)
Protected Sub selectedEmployee(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lnBTNDone As RadioButton = DirectCast(sender, RadioButton)
Dim row As GridViewRow = DirectCast(lnBTNDone.NamingContainer, GridViewRow)
Dim RowIndex As Integer = gvUser.EditIndex
Dim dt As New DataTable
dt = TryCast(Session("dbCache_User"), DataView).Table.Copy
Dim dv As New DataView(dt, "", "USR_IDNTY", DataViewRowState.OriginalRows)
Dim s As String = TryCast(gvUser.Rows(RowIndex).FindControl("lblUSR_IDNTY"), Label).Text
Dim idx As Integer = dv.Find(s)
dv(idx)("USR_ID") = row.Cells(1).Text
dv(idx)("NAME") = row.Cells(2).Text
dv(idx)("EMAIL") = row.Cells(3).Text
gvUser.DataSource = dv
gvUser.DataBind()
Me.mdlEmpListProcess.Hide()
End Sub
But I've encounter this error when debugging system.data.dataexception was unhandled by user code. Cannot set Name when passing the value.
dv(idx)("NAME") = row.Cells(2).Text
as per you comment NAME is readonly field error given at runtime than you make is readonly false as below , add same line in your code after reading value
dt.Columns["NAME"].ReadOnly = false
Working example
DataTable dataTable = new DataTable("dataTable");
DataColumn dataColumn = new DataColumn("dataColumn");
dataTable.Columns.Add(dataColumn);
// Add ten rows.
DataRow dataRow;
for (int i = 0; i < 10; i++)
{
dataRow = dataTable.NewRow();
dataRow["dataColumn"] = "item " + i;
dataTable.Rows.Add(dataRow);
}
dataTable.AcceptChanges();///add this line in your code
var dv = new DataView(dataTable, "", "dataColumn", DataViewRowState.OriginalRows);
int index = dv.Find("item 1");
if (index == -1)
Console.WriteLine("Product not found");
else
dv[index]["dataColumn"] = "item 1000";
I suggest first check you are able to find row or not
int idx = dv.Find(s);
if (index == -1)
{
Console.WriteLine ("Product not found");
}
else
{
dv[index]["columnname"] = "value";
}
I'm working with my GridView table in ASP.Net.
First when I click the Edit link button in my GridView I will be able to edit it with Textbox and dropdownlist.
Here's the image of my GridView.
So the Business Unit was enabled but the Division, Sub-Division, Classification and Sub-Classification are not.
My problem is that when I select the Business Unit, the dropdownlist Division should be enabled and be able to select the value according to the Business Unit that I selected and so on. So the next dropdownlist will be dependent on the first dropdownlist that you selected. I searched about this on the internet already and tried different solutions but I'm still confused and don't know how it will work.
Here's my code in ASP.Net
<asp:GridView ID="gvGroup" runat="server" AllowPaging="false"
AllowSorting="true" AutoGenerateColumns="False" BorderColor="Silver"
BorderWidth="1px" Height="104px" ShowFooter="true" ShowHeader="true"
style="margin-right: 0px" Width="77%">
<RowStyle Font-Names="Arial" Font-Size="9pt" HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderStyle-BackColor="#666666"
HeaderStyle-ForeColor="White" HeaderStyle-Width="10px" HeaderText=""
ItemStyle-Width="10px">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<HeaderStyle BackColor="#666666" ForeColor="White" />
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="140px" Visible="false">
<ItemTemplate>
<asp:Label ID="lblMasterID" runat="server" Text='<%#Bind("MASTERID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="140px" Visible="false">
<ItemTemplate>
<asp:Label ID="lblSEQID" runat="server" Text='<%#Bind("SEQID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Job Title" ItemStyle-Font-Size="9pt"
ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblJobTitle" runat="server" Text='<%#Bind("JOBTITLE") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJT" runat="server" CssClass="ehr_textbox"
Text='<%#BIND("JOBTITLE") %>' Width="140px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtJobTitleAdd" runat="server" Width="110px">
</asp:TextBox>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Business Unit"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblBusinessUnit" runat="server"
Text='<%#Bind("BUSINESS_UNIT") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvBusinessUnit" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlBusinessUnitAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Division" ItemStyle-Font-Size="9pt"
ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblDivision" runat="server" Text='<%#Bind("DIVISION") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvDivision" Enabled="false" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlDivisionAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Sub-Division"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblSubDivision" runat="server" Text='<%#Bind("SUB_DIVISION") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvSubDivision" Enabled="false" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSubDivisionAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Classification"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblClassification" runat="server"
Text='<%#Bind("CLASSIFICATION") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvClassification" Enabled="false" runat="server"
CssClass="ehr_dropdown" Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlClassification" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Sub-Classification"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblSubClassification" runat="server"
Text='<%#Bind("SUB_CLASSIFICATION") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvSubClassification" Enabled="false" runat="server"
CssClass="ehr_dropdown" Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSubClassification" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" ItemStyle-Font-Size="9pt">
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandName="Delete"
OnClientClick="return confirm('Do you want to delete?')" Text="Delete"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="btnAdd" runat="server" CommandName="Add"
OnClick="AddgvGroup" Text="Add"></asp:LinkButton>
</FooterTemplate>
<HeaderStyle BackColor="#666666" />
<ItemStyle Font-Size="9pt" />
</asp:TemplateField>
<asp:CommandField HeaderStyle-BackColor="#666666" HeaderStyle-Width="10px"
ItemStyle-Font-Size="9pt" ItemStyle-Width="10px" ShowEditButton="True">
<HeaderStyle BackColor="#666666" />
<ItemStyle Font-Size="9pt" />
</asp:CommandField>
</Columns>
<PagerStyle Font-Size="9pt" HorizontalAlign="Right" />
<EmptyDataTemplate>
<div style="width: 100%; font-size: 10pt; text-align: center; color: Red;">
No record found.
</div>
</EmptyDataTemplate>
<HeaderStyle BackColor="DarkGray" Font-Bold="True" Font-Names="Arial"
Font-Size="9pt" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
And here's my code in VB.Net
Protected Sub gvGroup_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvGroup.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
Dim ddlBusinessUnit As DropDownList = DirectCast(e.Row.FindControl("ddl_gvBusinessUnit"), DropDownList)
'bind dropdown-list
Dim sqlstr As String
Dim dt As DataTable = New DataTable()
sqlstr = "Select BUSINESS_UNIT from BUSINESS_UNIT_TBL"
dt = ehr_utils.DataTable(sqlstr)
ddlBusinessUnit.DataSource = dt
ddlBusinessUnit.DataTextField = "BUSINESS_UNIT"
ddlBusinessUnit.DataValueField = "BUSINESS_UNIT"
ddlBusinessUnit.DataBind()
Dim dr As DataRowView = TryCast(e.Row.DataItem, DataRowView)
ddlBusinessUnit.SelectedValue = dr("BUSINESS_UNIT").ToString()
End If
End If
End Sub
Any idea of how can I possibly do that? And I tried also the OnSelectedIndexChanged
First you need to add an OnSelectedIndexChanged to ddl_gvBusinessUnit and set AutoPostBack to true
<asp:DropDownList ID="ddl_gvBusinessUnit" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_gvBusinessUnit_SelectedIndexChanged">
And then in code behind
protected void ddl_gvBusinessUnit_SelectedIndexChanged(object sender, EventArgs e)
{
//cast the sender back to a dropdownlist
DropDownList dropDownList1 = sender as DropDownList;
//get the selectedvalue
string value = dropDownList1.SelectedValue;
//find the other dropdownlist in the correct row by using the editindex
DropDownList dropDownList2 = gvBusinessUnit.Rows[gvBusinessUnit.EditIndex].FindControl("DropDownList2") as DropDownList;
//do stuff with the other dropdownlist, like give it a color or add database values based on the first dropdownlist
dropDownList2.BackColor = Color.Red;
}
VB
Protected Sub ddl_gvBusinessUnit_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
'cast the sender back to a dropdownlist
Dim dropDownList1 As DropDownList = CType(sender,DropDownList)
'get the selectedvalue
Dim value As String = dropDownList1.SelectedValue
'find the other dropdownlist in the correct row by using the editindex
Dim dropDownList2 As DropDownList = CType(gvBusinessUnit.Rows(gvBusinessUnit.EditIndex).FindControl("DropDownList2"),DropDownList)
'do stuff with the other dropdownlist, like give it a color or add database values based on the first dropdownlist
dropDownList2.BackColor = Color.Red
End Sub
I'm working with vb.net and have followed a tutorial to set up a gridview to update/delete/insert records into a table. I have a footer row that isn't working for me. when I click my insert button the textboxes from the footer do not do the insert. I think I am missing something fundamental that is causing this. May the page is posting back before the sub grabs the text from the textboxes? My understanding of it is pretty but I feel this could be the issue because I have two radio buttons that I use to switch between SQL data adapters because I want to be able to work with different tables. I have these set to postback="true". Could that be my issue?
SqlDataAdapter:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Server=SQLSERVER;Database=DB1;UID=user;PWD=password"
DeleteCommand="delete from AVZV_LoginRequestSales where id = #id"
SelectCommand="select * from AVZV_LoginRequestSales"
UpdateCommand="update AVZV_LoginRequestSales set name=#name, role = #role, supervisor = #supervisor, location = #location, trainingSdt = #trainingSdt, productionSdt = #productionSdt, terminatedDate = #terminatedDate, notes = #notes where id = #id">
<DeleteParameters>
<asp:Parameter Name="id" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="firstname" />
<asp:Parameter Name="lastname" />
<asp:Parameter Name="role" />
<asp:Parameter Name="supervisor" />
<asp:Parameter Name="location" />
<asp:Parameter Name="trainingSdt" />
<asp:Parameter Name="productionSdt" />
<asp:Parameter Name="terminatedDate" />
<asp:Parameter Name="notes" />
</UpdateParameters>
</asp:SqlDataSource>
Datagrid:
<asp:GridView ID="gvMatrix" runat="server"
AutoGenerateColumns="false" enableviewstate="false" showheaderwhenempty="true"
AllowSorting="True" emptydatatext="No Agents Listed" showfooter="true"
OnDataBound="OnDataBound" DataKeyNames="id" >
<Columns>
<asp:TemplateField HeaderText="Full Name" InsertVisible="False" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newName" runat="server" placeholder="Full Name"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Role" InsertVisible="False" SortExpression="role">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("role") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtrole" runat="server" Text='<%# Bind("role") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newrole" runat="server" placeholder="Role"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Supervisor" InsertVisible="False" SortExpression="supervisor">
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("supervisor") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtsupervisor" runat="server" Text='<%# Bind("supervisor") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newsupervisor" runat="server" placeholder="Supervisor"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" InsertVisible="False" SortExpression="location">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("location") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtlocation" runat="server" Text='<%# Bind("location") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newlocation" runat="server" placeholder="Location"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Training Start Date" InsertVisible="False" SortExpression="trainingsdt">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("trainingsdt") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txttrainingsdt" runat="server" Text='<%# Bind("trainingsdt") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newtrainingsdt" runat="server" placeholder="Training Start Date"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False"/>
</asp:TemplateField>
<asp:TemplateField HeaderText="Production Start Date" InsertVisible="False" SortExpression="Productionsdt">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Productionsdt") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtProductionsdt" runat="server" Text='<%# Bind("Productionsdt") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newProductionsdt" runat="server" placeholder="Production Start Date"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False"/>
</asp:TemplateField>
<asp:TemplateField HeaderText="Terminated Date" InsertVisible="False" SortExpression="Terminateddate">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Terminateddate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtTerminateddate" runat="server" Text='<%# Bind("Terminateddate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newTerminateddate" runat="server" placeholder="Terminated Date"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False"/>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes" InsertVisible="False" SortExpression="notes">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("notes") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtNotes" runat="server" Text='<%# Bind("notes") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newNotes" runat="server" placeholder="Notes"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False"/>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<ItemTemplate>
<asp:Button ID="InsertRecord" runat="server" Text="Add Record" CommandName="Insert" />
</ItemTemplate>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" showeditbutton="true"/>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="deleteButton" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#3367a2" ForeColor="White" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#2461bf" Font-Bold="True" ForeColor="blue" HorizontalAlign="Center" />
<EditRowStyle CssClass="gridview_edit" />
<FooterStyle CssClass="GVFixedFooter" />
<AlternatingRowStyle BackColor="#eff3fb" ForeColor="#3367a2" HorizontalAlign="Center" />
</asp:GridView>
Code for Insert:
Protected Sub gvMatrix_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvMatrix.RowCommand
' Insert data if the CommandName == "Insert"
If e.CommandName = "Insert" AndAlso Page.IsValid Then
If rbSales.checked = True Then
SqlDataSource1.Insert()
Else
SqlDataSource2.Insert()
End If
End If
End Sub
Protected Sub SqlDataSource1_Inserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Inserting
' Programmatically reference Web controls in the inserting interface...
Dim NewName As TextBox = _
gvMatrix.FooterRow.FindControl("NewName")
Dim NewRole As TextBox = _
gvMatrix.FooterRow.FindControl("NewRole")
Dim NewSupervisor As TextBox = _
gvMatrix.FooterRow.FindControl("NewSupervisor")
Dim NewLocation As TextBox = _
gvMatrix.FooterRow.FindControl("NewLocation")
Dim Newtrainingsdt As TextBox = _
gvMatrix.FooterRow.FindControl("Newtrainingsdt")
Dim Newproductionsdt As TextBox = _
gvMatrix.FooterRow.FindControl("Newproductionsdt")
Dim Newterminateddate As TextBox = _
gvMatrix.FooterRow.FindControl("Newterminateddate")
Dim NewNotes As TextBox = _
gvMatrix.FooterRow.FindControl("NewNotes")
' Set the ObjectDataSource's InsertParameters values...
e.command.parameters("Name").Value = NewName.Text
e.command.parameters("role").Value = NewRole.Text
e.command.parameters("supervisor").Value = NewSupervisor.Text
e.command.parameters("location").Value = NewLocation.Text
e.command.parameters("trainingsdt").Value = Newtrainingsdt.Text
e.command.parameters("productionsdt").Value = Newproductionsdt.Text
e.command.parameters("terminatedDate").Value = Newterminateddate.Text
e.command.parameters("notes").Value = NewNotes.Text
End Sub
I know its a lot to look at but again I feel my issue might not be with the code so much as me missing a concept. Thanks in advance for any ideas/suggestions.
i have a gridview with pregenerated columns
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px"
CellPadding="4" GridLines="Horizontal">
<RowStyle BackColor="White" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="MenuID" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("MenuID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MenuParentID" >
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("MenuParentID") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Particulars">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Particulars") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="250px" />
</asp:TemplateField>
<asp:BoundField DataField="Options" HeaderText="Options"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Menu(M)" >
<ItemTemplate>
<asp:CheckBox ID="chkMenu" runat="server" OnCheckedChanged="chkMenu_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Add(A)">
<ItemTemplate>
<asp:CheckBox ID="chkAdd" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit(E)">
<ItemTemplate>
<asp:CheckBox ID="chkEdit" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete(D)">
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView>
want to fill the grid from database and check/uncheck the checkboxes from database condition. How to fill the gridview ...................................................................................
On which condition you want to check /Un-check the chechbox?
you can write code on RowDataBound event of grid.
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// here check the condition and find the check box of current row and set it checked/uncheked
}
}
I have a gridview as such
<asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." AlternatingRowStyle-CssClass="tr_dark" HeaderStyle-CssClass="header_req" BorderWidth="0px" GridLines="None" AllowPaging="true" PageSize="25" AllowSorting="false" Width="100%" ShowFooter="true" DataKeyNames="ProductId" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowCommand="gvShoppingCart_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Product Name" SortExpression="productName" HeaderStyle-CssClass="product" >
<ItemTemplate>
<asp:Label ID="ProductNameField" runat="server" Text='<%# Eval("description").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Pack Size" SortExpression="packSize" HeaderStyle-CssClass="packsize" >
<ItemTemplate>
<asp:Label ID="PackSizeField" runat="server" Text='<%# Eval("packSize").ToString()%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Stock" SortExpression="address" HeaderStyle-CssClass="stock">
<ItemTemplate>
<asp:Label ID="StockField" runat="server" Text='<%# DisplayStockLevel(Eval("StockIndicator").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Quantity" HeaderStyle-CssClass="quantity" >
<ItemTemplate>
<asp:TextBox runat="server" Width="30" ID="txtQuantity" Text='<%# Eval("Quantity") %>'></asp:TextBox>
<asp:TextBox runat="server" Visible="false" Width="30" ID="txtProductCode" Text='<%# Eval("ProductCode") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Actual Price" HeaderStyle-CssClass="actual" SortExpression="address">
<ItemTemplate>
<asp:Label ID="TradePriceField" runat="server" Text='<%# DisplayMoney(Eval("UnitPrice").ToString())%>'></asp:Label>
<asp:Label ID="TradePriceFieldHidden" runat="server" Text='<%# Eval("UnitPrice").ToString()%>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:BoundField DataField="TotalPrice" HeaderText="Total" HeaderStyle-CssClass="total" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
</Columns>
<Columns>
<asp:TemplateField HeaderText="" HeaderStyle-CssClass="remove">
<ItemTemplate>
<asp:ImageButton ImageUrl="~/img/icons/cross.gif" width="10" height="10" alt="Cancel" runat="server" ID="btnRemove" CommandName="Remove" CommandArgument='<%# Eval("ProductId") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
And now trying to add a footer rows dynamically ( because I need multiple footer rows)
Protected Sub gvShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvShoppingCart.RowDataBound
' If we are binding the footer row, let's add in our total
If e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(5).Text = "<strong>Total Cost:</strong>"
e.Row.Cells(6).Text = ShoppingCart.Instance.GetSubTotal().ToString("C")
End If
Dim grid As GridView = CType(sender, GridView)
''gets the current footer row to clone
Dim footer As GridViewRow = grid.FooterRow
Dim numCells = footer.Cells.Count
Dim newRow As New GridViewRow(footer.RowIndex + 1, -1, footer.RowType, footer.RowState)
''have to add in the right number of cells
''this also copies any styles over from the original footer
For i As Integer = 0 To numCells - 1
Dim emptyCell As New TableCell
emptyCell.ApplyStyle(grid.Columns(i).ItemStyle)
newRow.Cells.Add(emptyCell)
Next
newRow.Cells(5).Text = "Total Discount:"
newRow.Cells(6).Text = "55.00"
''add new row to the gridview table, at the very bottom
CType(grid.Controls(0), Table).Rows.Add(newRow)
End Sub
But getting the error
Object reference not set to an instance of an object.
Dim numCells = footer.Cells.Count
Any idea whats going wrong ?
The GridView FooterRow property is only set after the RowDataBound method has been called. You can access it in the DataBound method of the GridView.
Protected Sub gvShoppingCart_DataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim footerRow as GridViewRow = gvShoppingCart.FooterRow
End Sub
First of all your GridView Markup is incorrect. Define Columns just once like this.
<asp:GridView ID="gvShoppingCart" runat="server"
AlternatingRowStyle-CssClass="tr_dark"
AllowPaging="true"
AllowSorting="false"
AutoGenerateColumns="false"
BorderWidth="0px"
DataKeyNames="ProductId"
EmptyDataText="There is nothing in your shopping cart."
GridLines="None"
HeaderStyle-CssClass="header_req"
PageSize="25"
ShowFooter="true"
Width="100%"
OnRowCommand="gvShoppingCart_RowCommand"
OnRowDataBound="gvShoppingCart_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Product Name" SortExpression="productName" HeaderStyle-CssClass="product">
<ItemTemplate>
<asp:Label ID="ProductNameField" runat="server"
Text='<%# Eval("description").ToString() %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pack Size" SortExpression="packSize" HeaderStyle-CssClass="packsize">
<ItemTemplate>
<asp:Label ID="PackSizeField" runat="server"
Text='<%# Eval("packSize").ToString()%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Stock" SortExpression="address" HeaderStyle-CssClass="stock">
<ItemTemplate>
<asp:Label ID="StockField" runat="server"
Text='<%# DisplayStockLevel(Eval("StockIndicator").ToString()) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" HeaderStyle-CssClass="quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server"
Width="30"
Text='<%# Eval("Quantity") %>'>
</asp:TextBox>
<asp:TextBox ID="txtProductCode" runat="server"
Visible="false"
Width="30"
Text='<%# Eval("ProductCode") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actual Price" HeaderStyle-CssClass="actual" SortExpression="address">
<ItemTemplate>
<asp:Label ID="TradePriceField" runat="server"
Text='<%# DisplayMoney(Eval("UnitPrice").ToString())%>'>
</asp:Label>
<asp:Label ID="TradePriceFieldHidden" runat="server"
Text='<%# Eval("UnitPrice").ToString()%>'
Visible="false">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TotalPrice" HeaderText="Total"
HeaderStyle-CssClass="total" HeaderStyle-HorizontalAlign="Right"
DataFormatString="{0:C}" />
<asp:TemplateField HeaderText="" HeaderStyle-CssClass="remove">
<ItemTemplate>
<asp:ImageButton ID="btnRemove" runat="server"
ImageUrl="~/img/icons/cross.gif"
Width="10"
Height="10"
AlternateText="Cancel"
CommandName="Remove"
CommandArgument='<%# Eval("ProductId") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Secondly, you are getting error because you have not defined your FooterTemplate anywhere.