asp.net gridview index error - asp.net

I have a gridview which was working fine.
It has some hidden columns, then visible columns.
When I clicked on each row, VB code behind gets values from each column value in the selected row and puts the value in separate textboxes underneath the gridview, eg
txtProjectId.Text = GridView1.SelectedRow.Cells(2).Text.ToString ' project ID
All worked fine.
Find below gridview code (made shorter) as per requested.
<div id="div_GRIDVIEW" class="div_Gridview" clientidmode="Static" runat="server" >
<asp:GridView ID="GridView1" Width="1370px" runat="server" AllowPaging="True" AllowSorting="True" OnSelectedIndexChanged = "OnSelectedIndexChanged" OnRowDataBound = "OnRowDataBound"
cssclass="STD_GridView L4" AutoGenerateColumns ="False" DataKeyNames="Id" DataSourceID="SqlDataSource2" PageSize = "5" GridLines="Horizontal" EnableViewState="False" >
<rowstyle cssclass="GridRowStyle HoverableRow" />
<alternatingrowstyle cssclass="GridAlternateRowStyle HoverableRow" />
<PagerStyle BackColor="#56b7e9" ForeColor="White" HorizontalAlign="Center" width="1370px" Height="20px"/>
<SelectedRowStyle BackColor="#eeeeee" />
<SortedAscendingHeaderStyle BackColor="#56b7e9" />
<SortedDescendingHeaderStyle BackColor="#56b7e9" />
<Columns>
<asp:CommandField ShowSelectButton="true" SelectImageUrl="~/images/xxstd/update16.gif">
<ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" />
</asp:CommandField>
<asp:BoundField DataField="Id" HeaderText="ProjectMainId"><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
<asp:BoundField DataField="PDID" HeaderText="ProjectDetailsID"><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
<asp:BoundField DataField="PN" HeaderText="PCFlag" ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
<asp:BoundField DataField="DD" HeaderText="OCFlag" ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
<asp:BoundField DataField="OFF" HeaderText="OFFlag" ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
<asp:BoundField DataField="DCF" HeaderText="DCFlag" ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
<asp:BoundField DataField="OnCF" HeaderText="OnCFlag" ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
<asp:BoundField DataField="RF" HeaderText="ReadyFlag" ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
<asp:BoundField DataField="DF" HeaderText="DeclineFlag" ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
....many more hidden columns here (over 50)...
<asp:TemplateField SortExpression="PDID" >
<HeaderTemplate>
<asp:LinkButton ID="lbItem" runat="server" Text="Project#" CommandName="Sort" CommandArgument="Item" tabindex="1000"></asp:LinkButton><br />
<asp:TextBox runat="server" ID="txtS_PDID" CssClass="HF" OnTextChanged="txtItem_TextChanged"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<table >
<tr >
<td class="STD_normal" style="width:50px;font-weight:bold"><%#Eval("PDID")%></a></td>
</tr>
<tr>
<td class="STD_Normal_Grey" style="width:50px;"><%#Eval("Id")%> </td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="Item">
<HeaderTemplate>
<asp:LinkButton ID="lbItem" runat="server" Text="Project Name" CommandName="Sort" CommandArgument="Item" tabindex="1000"></asp:LinkButton><br />
<asp:TextBox runat="server" ID="txtS_PDID" CssClass="HF" OnTextChanged="txtItem_TextChanged"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<table >
<tr>
<td class="STD_normal" style="width:150px; font-weight:bold"><%#Eval("PN")%>
</td>
</tr>
<tr>
<td class="STD_Normal_Grey" style="width:150px"><%#Eval("DD", "{0:dd-MMM-yyyy}")%> </td>
</tr>
</table>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
And here is part of the code when row is selected....the index error occurs on Session("txtcurrentID") = GridView1.SelectedRow.Cells(1).Text.ToString :
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs) 'Handles gridview1.SelectedIndexChanged
Session("txtcurrentID") = GridView1.SelectedRow.Cells(1).Text.ToString
Session("txtcurrentPDID") = gridView1.SelectedRow.Cells(2).Text.ToString
Session("PCF") = GridView1.SelectedRow.Cells(3).Text.ToString
Session("OCF") = GridView1.SelectedRow.Cells(4).Text.ToString
Session("OFF") = GridView1.SelectedRow.Cells(5).Text.ToString
Session("DCF") = GridView1.SelectedRow.Cells(6).Text.ToString
Session("OnCF") = GridView1.SelectedRow.Cells(7).Text.ToString
end sub
So now when I click on each row, I get the following error:
Index was out of range. Must be non-negative and less than the size of the collection.
Everything works fine, the gridview displays correctly and the header filters look fantastic.
However I cannot longer click/select each row...
Any ideas ??

Related

Update control from .NET DataGrid on row select

I have what seems on the surface a really simple requirement. I want to update a label with a value from the data source when a user clicks a field in a datagrid control.
My page has an SqlDataSource control on it, which returns (for example) 6 columns. I display 5 of them in the datagrid, and want the sixth column shown in a label when the user selects a row.
I have tried various things, with limited success. One way I was convinced would work was to include the column in the datagrid, but set it to Visible="false". However, it turns out that if you do this, the value of row(5).text is ""... Not what I expected.
Any quick way of achieving this?
-- EDIT - Added in code samples --
<asp:GridView ID="gridL250Tickets" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Ridge" BorderWidth="2px" Caption="Last 250 Tickets" CellPadding="3" CellSpacing="1" DataKeyNames="TICKETID" DataSourceID="sqlSlxL250Tickets" AllowPaging="True" AllowSorting="True" HorizontalAlign="Center" Width="75%" PageSize="6">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="SUBJECT" HeaderText="Ticket" SortExpression="SUBJECT" />
<asp:BoundField DataField="RECEIVEDDATE" DataFormatString="{0:d}" HeaderText="Received" SortExpression="RECEIVEDDATE" />
<asp:BoundField DataField="COMPLETEDDATE" DataFormatString="{0:d}" HeaderText="Completed" SortExpression="COMPLETEDDATE" />
<asp:BoundField DataField="AREA" HeaderText="Area" SortExpression="AREA" />
<asp:BoundField DataField="CATEGORY" HeaderText="Category" SortExpression="CATEGORY" />
<asp:BoundField DataField="ISSUE" HeaderText="Issue" SortExpression="ISSUE" />
<asp:BoundField DataField="notes1" HeaderText="Notes" SortExpression="notes1" Visible="False" />
<asp:BoundField DataField="USERNAME" HeaderText="Ass. To" SortExpression="USERNAME" />
<asp:BoundField DataField="notes" HeaderText="Notes - l" SortExpression="notes" Visible="false" />
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
VB Code:
Private Sub gridL250Tickets_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gridL250Tickets.SelectedIndexChanged
Dim row As GridViewRow = gridL250Tickets.SelectedRow
Label1.Text = row.Cells(9).Text
End Sub
You could use a hidden <asp:TemplateField> with a label instead. I tested that out and it seems to work.
<asp:GridView ID="gridL250Tickets" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Ridge" BorderWidth="2px" Caption="Last 250 Tickets" CellPadding="3" CellSpacing="1" DataKeyNames="TICKETID" DataSourceID="sqlSlxL250Tickets" AllowPaging="True" AllowSorting="True" HorizontalAlign="Center" Width="75%" PageSize="6">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="SUBJECT" HeaderText="Ticket" SortExpression="SUBJECT" />
<asp:BoundField DataField="RECEIVEDDATE" DataFormatString="{0:d}" HeaderText="Received" SortExpression="RECEIVEDDATE" />
<asp:BoundField DataField="COMPLETEDDATE" DataFormatString="{0:d}" HeaderText="Completed" SortExpression="COMPLETEDDATE" />
<asp:BoundField DataField="AREA" HeaderText="Area" SortExpression="AREA" />
<asp:BoundField DataField="CATEGORY" HeaderText="Category" SortExpression="CATEGORY" />
<asp:BoundField DataField="ISSUE" HeaderText="Issue" SortExpression="ISSUE" />
<asp:BoundField DataField="notes1" HeaderText="Notes" SortExpression="notes1" Visible="False" />
<asp:BoundField DataField="USERNAME" HeaderText="Ass. To" SortExpression="USERNAME" />
<%--<asp:BoundField DataField="notes" HeaderText="Notes - l" SortExpression="notes" Visible="false" />--%>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblNotes" runat="server" Text='<%# Eval("notes") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
//...
Then just find the label in your event and use its text.
Private Sub gridL250Tickets_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gridL250Tickets.SelectedIndexChanged
Dim row As GridViewRow = gridL250Tickets.SelectedRow
Dim lblNotes As Label = row.FindControl("lblNotes")
Label1.Text = lblNotes.Text
End Sub
Instead of setting Visible = "False", try setting the actual CSS of the given column. Display = "none", and switch it back when the grid control is clicked.
Something along the lines of lblNotes.Style.Item("display") = "none". You could also do that in the Javascript if you didn't want to have a pageback.

Could not Update Text box control values

I am really frustrated to find a solution for my scenario. I have two gird when First grid is used show the shop information and the second grid is used to edit the offers related to the shop information. When the users edit the second grid i will just update the text box values related to the grid selection row. in my code behind file i can see the data fetching from the grid and assigning into the text boxes but when the function call (imgEdit_click) finished the page does not show the values. Editing functionality can be done in many ways but my scenario is what i explained earlier . I have the checked page there is no Postback action has been called after the method I could not find the solution can anyone help me to figure it out.
Following are my source and code behind codes.
My design Source :
<div class="field">
<asp:TextBox ID="txtareaOfferDesc" runat="server" TextMode="MultiLine" ></asp:TextBox>
</div>
<div class="field">
<asp:TextBox ID="txtTimeStarts" runat="server" CssClass="textfield"></asp:TextBox>
<cc1:CalendarExtender
ID="CalendarExtender2"
runat="server"
TargetControlID="txtTimeStarts"
CssClass="CalendarCSS">
</cc1:CalendarExtender>
<div class="datefld">
<label class="name">Offer end date/time (optional)</label>
<div class="field">
<asp:TextBox ID="txtTimeEnd" runat="server" CssClass="textfield"></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender4" runat="server" TargetControlID="txtTimeEnd"
CssClass="CalendarCSS">
</cc1:CalendarExtender> `
<asp:GridView ID="gvShopDeal" runat="server" AutoGenerateColumns="false"
CssClass="tblexistoffer" DataKeyNames="ShopID" AllowPaging="True"
AllowSorting="True">
<Columns>
<asp:TemplateField>
<HeaderTemplate >
<asp:CheckBox ID="chkHeader" runat="server" />
<asp:Label ID="lblSelectAll" Text="Select All" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate >
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ShopID" HeaderStyle-Width="10%" HeaderText="Shop ID" />
<asp:BoundField DataField="ShopName" HeaderStyle-Width="40%" HeaderText="Shop Name" />
<asp:BoundField DataField="Street" HeaderStyle-Width="40%" HeaderText="Street" />
<asp:BoundField DataField="City" HeaderText ="City" />
</Columns>
</asp:GridView>
<asp:Button ID="btnCreateDeal" runat="server" Text="Create Offer"
CssClass="grnbtn" OnClientClick="return CheckDealValidation(this)" onclick="btnCreateDeal_Click"></asp:Button>
<asp:Button ID="btnDefCancel" runat="server" Text="Cancel" CssClass="greybtn"></asp:Button>
<asp:UpdatePanel ID="UpdateExistingOffer" runat="server">
<ContentTemplate>
<asp:GridView ID="gvExistingOffers" runat="server" CssClass="tblexistoffer"
AutoGenerateColumns="false" DataKeyNames="OfferID" AllowPaging="True"
AllowSorting="True">
<Columns>
<asp:BoundField DataField="OfferID" HeaderText="OfferID" />
<asp:BoundField DataField="Description" HeaderText="OfferName" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="OfferType" HeaderText="OfferType" />
<asp:BoundField DataField="StartDate" HeaderText="StartDate">
<ItemStyle CssClass="Hide" />
<HeaderStyle CssClass="Hide" />
</asp:BoundField>
<asp:BoundField DataField="EndDate" HeaderText="EndDate" >
<ItemStyle CssClass="Hide" />
<HeaderStyle CssClass="Hide" />
</asp:BoundField>
<asp:TemplateField HeaderText="Edit" HeaderStyle-Width="5%">
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" ImageUrl="~/Merchant/images/edit.jpg" runat="server" Width="25" Height="25" onclick="imgbtnEdit_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Code Behind File :
protected void imgbtnEdit_Click(object sender, ImageClickEventArgs e)
{
ImageButton btndetails = sender as ImageButton;
GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
fferIDForShop = Convert.ToInt32(gvExistingOffers.DataKeys[gvrow.RowIndex].Value);
ShopList objShopID = ShopService.GetShopID(OfferIDForShop);
(txtareaOfferDesc.Text) = gvrow.Cells[1].Text.Trim();
txtTimeStarts.Text = gvrow.Cells[4].Text;
txtTimeEnd.Text = gvrow.Cells[5].Text;
}
Thanks
Vijay
Issue is because textboxes are out of Update Panel, So just put everything in update panel it will start functioning or just comment out update panel and then try the same thing.

AjaxControlToolkit - GridView wont select - don't understand viewstate

Overview:
I have two text boxes at the top of this form for inputting a beginning/ending date.
In the middle of the form I have a gridview that is populated with rows based on those dates.
At the bottom of the form I have a report generated based on which row is selected in the grid.
I selected the BeginningDate and the EndingDate and the gridview becomes populated (working). When I click the "Select" button on a row in the grid something is happening that makes it break. The row isn't selected. I know this because the CSS thats usually applied (by default) to a selected row in the gridview is not applied.
Additionally in the code-behind I'm setting...
lblGridError.Text = gridJobsReport.SelectedIndex.ToString()
The page load even has the same code, so the label reads "-1" when the page loads but never changes when I click the select buttons on the gridview. Something is breaking my Async postbacks. I wish I understood viewstate better, I'm always suspicious of it being the culprit but honestly I have no idea.
<%# Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="BidReport.aspx.vb" Inherits="BidReport" %>
<%# Register assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<ajaxToolkit:ToolkitScriptManager ID="tsmgrJobsReport" runat="server"
CombineScripts="True">
</ajaxToolkit:ToolkitScriptManager>
<p>
Enter a date range to populate the Jobs Grid.</p>
<asp:UpdatePanel ID="uPanelForm" runat="server">
<ContentTemplate>
<table>
<tr>
<td style="text-align: right">
<asp:Label ID="lblBeginningDate" runat="server" Text="Beginning Date:"></asp:Label></td>
<td>
<asp:TextBox ID="txtBeginningDate" runat="server" AutoPostBack="True"
CausesValidation="True"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="txtBeginningDate_CalendarExtender"
runat="server"
TargetControlID="txtBeginningDate">
</ajaxToolkit:CalendarExtender>
</td>
<td>
<asp:RequiredFieldValidator ID="rfvBeginningdate" runat="server"
ControlToValidate="txtBeginningDate"
ErrorMessage="You must enter the Beginning Date" Font-Bold="True"
ForeColor="Red">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revBeginningDate" runat="server"
ControlToValidate="txtBeginningDate"
ErrorMessage="Posted Date must be in the format "DD/MM/YYYY""
Font-Bold="True" ForeColor="Red"
ValidationExpression="^([1-9]|1[012])/([1-9]|[12][0-9]|3[01])/(20\d\d)$">*</asp:RegularExpressionValidator>
</td>
<td rowspan="2">
<asp:CompareValidator ID="cvBeginDateLTEndDate" runat="server"
ControlToCompare="txtEndingDate" ControlToValidate="txtBeginningDate"
ErrorMessage="Beginning Date must be earlier than the Ending Date."
Font-Bold="True" ForeColor="Red" Operator="LessThan">*</asp:CompareValidator>
</td>
</tr>
<tr>
<td style="text-align: right">
<asp:Label ID="lblEndingDate" runat="server" Text="Ending Date:"></asp:Label></td>
<td>
<asp:TextBox ID="txtEndingDate" runat="server" AutoPostBack="True"
CausesValidation="True"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="txtEndingDate_CalendarExtender"
runat="server" TargetControlID="txtEndingDate">
</ajaxToolkit:CalendarExtender>
</td>
<td>
<asp:RequiredFieldValidator ID="rfvEndingDate" runat="server"
ControlToValidate="txtEndingDate"
ErrorMessage="You must enter the EndingDate Date" Font-Bold="True"
ForeColor="Red">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revEndingDate" runat="server"
ControlToValidate="txtEndingDate"
ErrorMessage="Posted Date must be in the format "DD/MM/YYYY""
Font-Bold="True" ForeColor="Red"
ValidationExpression="^([1-9]|1[012])/([1-9]|[12][0-9]|3[01])/(20\d\d)$">*</asp:RegularExpressionValidator>
</td>
</tr>
</table>
<asp:ValidationSummary ID="valsumDates" runat="server" ForeColor="Red" />
<p>Click or Tap the "SELECT" button in the right column next to the Job you want and
a report will be generated below.</p>
<asp:GridView ID="gridJobsReport" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="PostingID" DataSourceID="sqlDSReportGrid"
ForeColor="#333333" GridLines="Horizontal"
ShowHeaderWhenEmpty="True" Width="100%">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="PostingID" HeaderText="PostingID" ReadOnly="True"
SortExpression="PostingID" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Department" HeaderText="Department"
SortExpression="Department" />
<asp:BoundField DataField="JobName" HeaderText="JobName"
SortExpression="JobName" />
<asp:BoundField DataField="Shift" HeaderText="Shift" SortExpression="Shift" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Needs" HeaderText="Needs"
SortExpression="Needs" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="PostedDate" HeaderText="PostedDate"
SortExpression="PostedDate" DataFormatString="{0:d}" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="PostedTime" HeaderText="Time"
SortExpression="PostedTime" DataFormatString="{0:t}" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ClosingDate" HeaderText="ClosingDate"
SortExpression="ClosingDate" DataFormatString="{0:d}" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ClosingTime" HeaderText="Time"
SortExpression="ClosingTime" DataFormatString="{0:t}" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:CommandField ButtonType="Button" CausesValidation="True"
ShowSelectButton="true" />
</Columns>
<EditRowStyle BackColor="#999999" />
<EmptyDataRowStyle HorizontalAlign="Center" />
<EmptyDataTemplate>
There is currently no data to display based on the date range provided.
</EmptyDataTemplate>
<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" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<br />
<asp:Label ID="lblGridError" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<ajaxToolkit:UpdatePanelAnimationExtender ID="uPanelForm_UpdatePanelAnimationExtender"
runat="server" TargetControlID="uPanelForm">
</ajaxToolkit:UpdatePanelAnimationExtender>
<p>You can print or save the report using the buttons at the top of the report.</p>
<asp:UpdatePanel ID="uPanelJobsReport" runat="server">
<ContentTemplate>
<rsweb:ReportViewer ID="rvJobsReport" runat="server" Width="100%">
</rsweb:ReportViewer>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gridJobsReport"
EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:SqlDataSource ID="sqlDSReportGrid" runat="server"
ConnectionString="<%$ ConnectionStrings:JobsDB %>"
SelectCommand="SELECT dbo.tblJobPostings.PostingID, dbo.tblDepartments.Department, dbo.tblJobs.JobName,
dbo.tblJobPostings.Shift, dbo.tblJobPostings.Needs, dbo.tblJobPostings.PostedDate,
dbo.tblJobPostings.PostedTime, dbo.tblJobPostings.ClosingDate, dbo.tblJobPostings.ClosingTime
FROM dbo.tblJobPostings
INNER JOIN dbo.tblJobs ON dbo.tblJobPostings.JobID = dbo.tblJobs.JobID
INNER JOIN dbo.tblDepartments ON dbo.tblJobPostings.DeptID = dbo.tblDepartments.DeptID
WHERE dbo.tblJobPostings.ClosingDate >= #BeginningDate
AND dbo.tblJobPostings.ClosingDate <= #EndingDate
ORDER BY dbo.tblJobPostings.ClosingDate Asc">
<SelectParameters>
<asp:ControlParameter ControlID="txtBeginningDate" Name="BeginningDate"
PropertyName="Text" ConvertEmptyStringToNull="False" DefaultValue="" />
<asp:ControlParameter ControlID="txtEndingDate" Name="EndingDate"
PropertyName="Text" ConvertEmptyStringToNull="False" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sqlDSJobReport" runat="server"></asp:SqlDataSource>
</asp:Content>
I really don't understand why this worked, but I decided to try handling the dates manually with session variables and its working perfectly now. It doesn't matter if I mess with the session state on the update panels or any of the form elements it seems to work no matter what now.
I guess that after the initial asyncpostback the sqldatasource wasn't reading the values in the textboxes, or something. If anyone can explain what was going on in another "Answer Question" section I'll accept it.
If Page.IsPostBack Then
Session("sBeginningDate") = txtBeginningDate.Text
Sessoin("sEndingDate") = txtEndingDate.Text
txtBeginningDate.Text = Session("sBeginningdate")
txtEndingDate.Text = Session("sEndingDate")
Else
Session("sBeginningDate") = Now().AddDays(-14)
Session("sEndingDate") = Now()
txtBeginningDate.Text = Session("sBeginningDate").ToShortDateString
txtEndingDate.Text = Session("sEndingDate").ToShortDateString
End If

Question about ASP.NET gridviews

I have a gridview. Is it possible to modify the gridview so that I can have multiple rows in the column headers? For example, the following code generates the table in the picture below.
<asp:GridView ID="productsGridView" Runat="server" DataSourceID="productDataSource" AutoGenerateColumns="False" AllowSorting="True" BorderWidth="2px"
BackColor="White" GridLines="None" CellPadding="3" CellSpacing="1" BorderStyle="Ridge" BorderColor="White" AllowPaging="True">
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#C6C3C6"></PagerStyle>
<HeaderStyle ForeColor="#E7E7FF" Font-Bold="True" BackColor="#4A3C8C"></HeaderStyle>
<Columns>
<asp:BoundField HeaderText="Product" DataField="ProductName" SortExpression="ProductName"></asp:BoundField>
<asp:BoundField HeaderText="Unit Price" DataField="UnitPrice" SortExpression="UnitPrice" DataFormatString="{0:c}">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Units In Stock" DataField="UnitsInStock" SortExpression="UnitsInStock" DataFormatString="{0:d}">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Quantity Per Unit" DataField="QuantityPerUnit"></asp:BoundField>
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True" BackColor="#9471DE"></SelectedRowStyle>
<RowStyle ForeColor="Black" BackColor="#DEDFDE"></RowStyle>
</asp:GridView>
This table only has one row in the column header. What I am looking for is something like this:
Any Ideas on how I can achieve this? Is this even possible?
If you use a TemplateField instead, you can control the header template as well, which can be custom ASPX markup. The downside is that you have to display the data manually with Labels instead of using the simpler BoundField properties. However, this also allows you to layout the data in a custom layout to fit with the heading:
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Weight<br />
Date | Time<br />
Product
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("Weight") %>'></asp:Label><br />
<asp:Label runat="server" Text='<%#Eval("Date") %>'></asp:Label> |
<asp:Label runat="server" Text='<%#Eval("Time") %>'></asp:Label><br />
<asp:Label runat="server" Text='<%#Eval("Product") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>

set column width of a gridview in asp.net

I dragged and dropped a GridView from toolbox to Aspx page.the design code looks like this
<asp:GridView ID="gridview1" runat="server" style="text-align:center;width: 1327px;"
CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True"
EnableSortingAndPagingCallbacks="True" PageSize="50" AutoGenerateEditButton="True">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"
Height="12px" VerticalAlign="Bottom" Width="12px" Wrap="False" />
<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" />
</asp:GridView>
I have a column by name Address in database.The table is bound to this grid view using following VB.NET code
Dim ds as Data.DataSet
Dim da As New OleDbDataAdapter("select col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,Address from table, con)
da.Fill(ds)
gridviewrealestate.DataSource = ds
gridviewrealestate.DataBind()
Now,When the gridview is displayed,the columns are very close to each other due to large information present in Address Column.I want to set the Address Column to a particular width.
Please help me how to do this.
<asp:GridView ID="GridView1" runat="server">
<HeaderStyle Width="10%" />
<RowStyle Width="10%" />
<FooterStyle Width="10%" />
<Columns>
<asp:BoundField HeaderText="Name" DataField="LastName"
HeaderStyle-Width="10%" ItemStyle-Width="10%"
FooterStyle-Width="10%" />
</Columns>
</asp:GridView>
I know this is an old Question, but it popped up when I was looking for a solution to the same issue, so I thought that I would post what worked for me.
<asp:BoundField DataField="Description" HeaderText="Bond Event" ItemStyle-Width="300px" />
I used the ItemStyle-Width attribute on my BoundField and it worked very nicely I haven't had any issues yet.
I didn't need to add anything else to the rest of the code to make this work either.
Set width
HeaderStyle-width
for Example HeaderStyle-width="10%"
This what worked for me. set HeaderStyle-Width="5%", in the footer set textbox
width Width="15",also set the width of your gridview to 100%. following is the one of the column of my gridview.
<asp:TemplateField HeaderText = "sub" HeaderStyle-ForeColor="White" HeaderStyle-Width="5%">
<ItemTemplate>
<asp:Label ID="sub" runat="server" Font-Size="small" Text='<%# Eval("sub")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_sub" runat="server" Text='<%# Eval("sub")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt_sub" runat="server" Width="15"></asp:TextBox>
</FooterTemplate>
You need to convert the column into a 'TemplateField'.
In Designer View, click the smart tag of the GridView, select-> 'Edit columns'.
Select your column you wish to modify and press the hyperlink converting it to a template. See screenshot:
Converting column to template.
Note: Modifying your datasource might regenerate the columns again, so it might make changes to your template columns.
Ref: https://msdn.microsoft.com/en-us/library/bb288032.aspx
Add HeaderStyle-Width and ItemStyle-width to TemplateFiels
<asp:GridView ID="grdCanceled" runat="server" AutoGenerateColumns="false" OnPageIndexChanging="grdCanceled_PageIndexChanging" AllowPaging="true" PageSize="15"
CssClass="table table-condensed table-striped table-bordered" GridLines="None">
<HeaderStyle BackColor="#00BCD4" ForeColor="White" />
<PagerStyle CssClass="pagination-ys" />
<Columns>
<asp:TemplateField HeaderText="Mobile NO" HeaderStyle-Width="10%" ItemStyle-Width="10%">
<ItemTemplate>
<%#Eval("mobile") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-Width="10%" ItemStyle-Width="10%">
<ItemTemplate>
<%#Eval("name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" HeaderStyle-Width="10%" ItemStyle-Width="10%">
<ItemTemplate>
<%#Eval("city") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reason" HeaderStyle-Width="25%" ItemStyle-Width="25%">
<ItemTemplate>
<%#Eval("reson") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Agent" HeaderStyle-Width="10%" ItemStyle-Width="10%">
<ItemTemplate>
<%#Eval("Agent") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" HeaderStyle-Width="10%" ItemStyle-Width="10%">
<ItemTemplate>
<%#Eval("date","{0:dd-MMM-yy}") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DList" HeaderStyle-Width="10%" ItemStyle-Width="10%">
<ItemTemplate>
<%#Eval("service") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EndDate" HeaderStyle-Width="10%" ItemStyle-Width="10%">
<ItemTemplate>
<%#Eval("endDate","{0:dd-MMM-yy}") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="5%" ItemStyle-Width="5%">
<ItemTemplate>
<asp:CheckBox data-needed='<%#Eval("userId") %>' ID="chkChecked" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
There are two steps:
You must set an appropriate width for GridView like this: Width="2000px".
You can set width of Colum by setting [ItemStyle-Width] Like this: ItemStyle-Width="300px".
** You can set width by setting fixed Pixels like "150 px" or by percentage like"10%".
<asp:BoundField DataField="ElementName" HeaderText="RBI GL Name" ItemStyle-Horizontal ItemStyle-Width="50px" HeaderStyle-Width="50px"/>

Resources