Gridview not allowing paging - asp.net

I have a gridview on a asp web page and the formatting works fine except when I add AllowPaging or if I set the location of the gridview. Here is the gridview code that I am using.
<asp:GridView
ID="gridview01"
runat="server"
SkinId="Professional"
AllowPaging="true"
CellPadding="4"
EnableModelValidation="True"
ForeColor="#333333"
GridLines="None"
Height="179px"
Width="496px"
style="margin-right: 0px">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
The top portion is just laid out like that so that I could read it easier. The gridview is populated when a button is pressed. There is code behind the button that calls the query based on the input of two drop down lists.

Use THis Code at BackEnd For Paging in Grid or ListView
protected void Grid_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
GridDataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
BindGridView();
}

Related

How to update gridview through design code/ aspx page code

I'm trying to update GridView every time I update, delete or insert any record. Now I have provided sql data source code for GridView in Design Code not form code. Now how can I update it from there ? When I write GridView1.databind() on form code, it says
> Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.
Can someone tell me how to DataBind in design view, to update GridView every time I insert/update/delete record ?
Here is GridView1 Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" Width="284px">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="surname" HeaderText="surname" SortExpression="surname" />
<asp:BoundField DataField="amount" HeaderText="amount" SortExpression="amount" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myDbConnectionString %>" SelectCommand="SELECT [name], [surname], [amount] FROM [Table1]"></asp:SqlDataSource>
Set the DataSourceID to null.
GridView1.DataSourceID = null;
GridView1.DataSource = dt;
GridView1.DataBind();
That error was coming because data was being binded on both Design view code as well as web form backend code.
I simply added GridView1.DataSourceID = null;
and that did the job.

Getting value from selected row in gridview

I'm a novice vb programmer trying to retrieve the value of the selected row in a gridview. Haven't been able to as of yet. I've searched all the forums, none of the answers have worked for me.
Here is the html portion of the code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" Width="1032px"
EnableModelValidation="True">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Date" >
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" onclick ="LinkButton1_Click"
CommandName="DrillTP" Text='<%#Eval("transit_date")%>'> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="transit_date" HeaderText="Date" Visible="false" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="day_type" HeaderText="DayType" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="current_route_id" HeaderText="Line" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="bs_lname" HeaderText="Location" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<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" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
in visual basic, I'm trying to capture the selected date and place that info in a label, but haven't been able to yet. any suggestions? I have this:
Protected Sub LinkButton1_Click(sender As Object, e As EventArgs) '(sender As Object, e As DataGridCommandEventArgs)
Label1.Text = GridView1.SelectedRow.DataItem
End Sub
clearly, I'm missing something, any help is appreciated. Also, please let me know if I can post this question more effectively.
thanks!!
You need to use the property Value, and perhaps use the ToString method.
GridView1.SelectedRows(0).Value.ToString()

Gridview error message

when I go onto my product search, the text from my EmptyDataTemplate shows up before I have even searched! can anyone shed light on this situation?
<asp:GridView ID="gvProducts" runat="server" CellPadding="4" DataSourceID="sdsProducts" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
<EmptyDataTemplate>
<asp:Label Text="Sorry, no results found" SkinID="ErrorMessage" runat="server" />
</EmptyDataTemplate>
</asp:GridView>
Thanks.
This is because of how you bind your data source. When you declare a data source like DataSourceID="sdsProducts" in your GridView's markup, that data source will bind its data to your GridView each time the page loads.
So when your page loads for the first time, your data source retrieves an empty set of data and binds it to the GridView, showing your EmptyDataTemplate.
An alternative approach is to bind via the code behind. This way you tell it exactly when you want it to bind, avoiding any binding on the first page load if you so choose.

How can i make a hyperlink column of a gridview containing pdf or doc files to either open in acrobat reader or in a new tab in browser?

This is my gridview
<asp:GridView ID="MathGridView" Width="1230px" runat="server" CellPadding="20"
ForeColor="#333333" AutoGenerateColumns="false"
GridLines="None">
<Columns>
<asp:HyperLinkField HeaderText="Name" DataNavigateUrlFields="Name" DataTextField="Name" HeaderStyle-HorizontalAlign="Left"/>
</Columns>
<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" />
<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>
There is one hyperlink column present called Name. Now what i want is when i click any content of name(i.e. Any file with extension .pdf or.doc), I want to open that content in either acrobat reader(in case of pdf) or in a new tab in browser.Here the contents i.e.files are not stored in a database. How can i do this??
OK - what you're missing is pointing the hyperlink to correct location; add this to your asp:HyperLink element:
DataNavigateUrlFormatString="~/Books/Math/{0}"

How to call a form from a gridview button click?

I am using a Gridview in my application which contains button field as below.
<asp:GridView ID="grvAccrualData" runat="server" AutoGenerateColumns="False" ForeColor="Black" Font-Names="Arial"
BackColor="#B10633" __designer:wfdid="w9" AllowPaging="True" Width="100%" OnRowCommand="grvAccrualData_RowCommand">
<PagerSettings FirstPageText="&lt;&lt; First" LastPageText="Last &gt;&gt;"
Mode="NextPreviousFirstLast" NextPageText="Next &gt;" PreviousPageText="Prev &lt;">
</PagerSettings>
<FooterStyle BackColor="#B10633" Font-Bold="True" ForeColor="White"></FooterStyle>
<RowStyle Font-Names="Arial" Font-Size="8pt" HorizontalAlign="Left" VerticalAlign="Middle" BackColor="Ivory" />
<Columns>
<asp:BoundField DataField="AccrlHeaderAccrualNo" HeaderText="Accrual Number">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle>
</asp:BoundField>
<asp:ButtonField ButtonType="button" CommandName="AccrualDetail" HeaderText="Debit Generation" Text="Account" />
</Columns>
<PagerStyle HorizontalAlign="Center" BackColor="#B10633" ForeColor="Black"></PagerStyle>
<HeaderStyle BackColor="#B10633" Font-Names="Arial" Font-Size="8pt" ForeColor="White">
</HeaderStyle>
<AlternatingRowStyle BackColor="#FFC0C0"></AlternatingRowStyle>
</asp:GridView>
And my server side code is :
protected void grvAccrualData_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AccrualDetail")
{
}
}
In this server code I want to call a form or a webpage on button click.
Use
Response.Redirect("~/WebPage.aspx");
Or if you want to pass parameter along with this means then use QueryString.

Resources