I have a Gridview with 3 columns (CompanyName, Volume, counter) I would like to have the counter column be all buttons and when a user clicks the button it increments the counter by 1. I'm not sure how to properly setup the update command. Here is what I have so far:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="Volume" HeaderText="Volume"
SortExpression="Volume" />
<asp:ButtonField DataTextField="counter" HeaderText="counter"
ButtonType="button"/>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LiquorStoreConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:LiquorStoreConnectionString1.ProviderName %>"
SelectCommand="SELECT [CompanyName], [Volume], [counter] FROM [Company]">
</asp:SqlDataSource>
All I need it to do is increment by 1 every time the button is pressed on that specific row.
You can try this:
... UpdateCommand="Update COMPANY SET counter=#Counter +1 WHERE CopmanyName=#Company"
<columns>
<asp:BoundField HeaderText="counter" DataField="Counter" />
</columns>
Make sure you have the bound field names correct.
I haven't tested to see if this + 1 works with a bound field. If it does not, then we'll have to use OnRowCommand to update the database manually, then you can re-bind the view and see updated counter. More importantly, I don't see a nice primary key for your Company so you can do the update... So my sample is using the CompanyName, and that is danger danger.
do you want to update the database?
You can catch the button click event in gridview row command .
Take a look at link given below
http://msdn.microsoft.com/en-us/library/bb907626.aspx
Related
Currently my gridview shows all registered users of my game, when anyone logs in.
i would like it to just show the row of user who has just logged, specified by their username.
here is the problem, so as the user Nick is currently logged in it should just show the row where username = Nick.
How can i do this?
<asp:SqlDataSource ID="SqlDataSource_Game" runat="server" ConnectionString="<%$ ConnectionStrings:\\MAC\HOME\DESKTOP\NIMV1.MDFConnectionString %>" SelectCommand="SELECT [UserName], [Won], [Lost], [Played] FROM [Table]"></asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="UserName" DataSourceID="SqlDataSource_Game">
<Columns>
<asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" SortExpression="UserName" />
<asp:BoundField DataField="Won" HeaderText="Won" SortExpression="Won" />
<asp:BoundField DataField="Lost" HeaderText="Lost" SortExpression="Lost" />
<asp:BoundField DataField="Played" HeaderText="Played" SortExpression="Played" />
</Columns>
</asp:GridView>
You need to maintain login-status whenever user logged in loggedout
Add a column as loginStatus
When user logged in set col loginStatus = 1
When user logout set col loginStatus = 0
On pageload where you bind gridview make a query like
Select * from tableName where loginStatus=1
You can directly hide all the unnecessary rows, checking on the fly for the one to show. I mean something like (in a kind of pseudocode, some typos possible):
foreach (DataGridViewRow myrow in GridView1.Rows)
{
myrow.Visible=false;
if(myrow.Cells[0].Value == "Nick")
{
myrow.Visible=true;
}
}
I'm connecting to an access database using the AccessDataSource for a course listing. I'm primarily relying on the prebuilt controls - GridView.
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="AccessDataSource1" EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:BoundField DataField="category" HeaderText="category" SortExpression="category" />
</Columns>
</asp:GridView>
My problem is I want to filter the data based on the Category through a dropdown list. The dropdown list does not show non-distinct though.
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="AccessDataSource1" DataTextField="category" DataValueField="category">
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="C:\Users\x\Documents\Visual Studio 2013\WebSites\WebSite2\courses.mdb" SelectCommand="SELECT [title], [category] FROM [mainclasslist]"></asp:AccessDataSource>
I need to be able to create a view that show this:
Couse Category1
Course 1
Course 2
Course Category 2
course 1
so etc...
Should I be using 2 datasources? The gridview should be filtered based on the dropdown. But on the initial load I need all the courses to be visible.
I use GridView to display data from DB. First I query data from DB to list<list<string>>, sort use linq, copy this array to DataTable and bind DataTable to GridView. But if in DataTable more then 1000 rows - it works very long or not works (error in browser).
How to fix this?
UPDATE i use rowspan in columns and create a delete buttons to all rows, and if i use paging wiil it works?
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnDataBinding="GridView1_DataBinding" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting" >
<Columns>
<asp:BoundField DataField="Number" ItemStyle-Width="200px" > <ItemStyle Width="200px" > </ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Name" ItemStyle-Width="200px" > <ItemStyle Width="200px" > </ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Nameoid" ItemStyle-Width="200px" > <ItemStyle Width="200px" > </ItemStyle>
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="return DeleteConfirm();" OnClick="Button2_Click" />
<asp:HiddenField ID="HiddenField2" runat="server" Value='<%#Bind("Number") %>' />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
It seems like you are doing lots of server side code in one round trip time. You should make it lesser so that page could show the result in grid view.
Here are few things on which you should work:
There is no need to store first in list then data table and then sorting. You can directly use the DATAVIEW class to sort the result from DB.
I think you should not display all 1000 record in grid view in a single page. You should use PAGING in grid view. This is also in built functionality of grid view by this you can show number of record in a one grid view page as you want..
For reference how to do this there is complete example. Follow the below link
http://www.dotnetgallery.com/kb/resource12-How-to-implement-paging-and-sorting-in-aspnet-Gridview-control.aspx
UPDATE:
If you want to delete a row in gridview with a link button on each row. Then follow the below article
enter link description here
You can put the UpdatePanel outside of gridview. First try to use the above code (from link), then try to add update panel. If you feel any difficulties ask here.
My suggestion would be to use Lazy loading pattern with your GridView .
You can refer to :
LazyLoadUpdatePanelusingTimerControlAJAX
or you can implement custom paging
Custompaging
You can use paging in your grid view having grid size of say 10,20,30 to accommodate large amount of data so that the page doesn't crash.
Try referring to this example
http://www.codeproject.com/Articles/106678/Display-Large-Amount-of-Data-in-GridView-with-Sear
I've created a wallboard application to display outstanding support calls for my ICT department. I've bound a number of gridviews to sqldatasources which execute a stored procedure. This is automated via asp.net ajax controls and partially refreshes the page/data every 30 seconds.
At the moment, when the number of records in the gridview goes over 9, the gridview automatically pages and shows the number of pages in the bottom right hand corner. The helpdesk can then VNC to the box which controls the screen and manually click to see what's on the next page.
What I am after is a way to programmatically (using the c# code-behind file) changing the current displayed page after 10/15 seconds or so, obviously if this is possible in the scope of the gridview. I trailed using javascript (and failed at jquery) of scrolling the gridview within a div, however this didn't work as expected.
Can anyone point me in the right example? I can't find anyone else querying this functionality via a quick Google. Any help/advice of how to fix this issue would be greatly appreciated!!
Gridview Code:
<asp:GridView ID="GridView1" ShowHeader="False" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
GridLines="None" CellPadding="2" Font-Size="35pt" AllowPaging="True" PageSize="9">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID">
<ItemStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="ASSIGNEES" HeaderText="ASSIGNEES" SortExpression="ASSIGNEES">
<ItemStyle Width="32%" Wrap="false"/>
</asp:BoundField>
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title">
<ItemStyle Width="53%" Wrap="false"/>
</asp:BoundField>
</Columns>
</asp:GridView>
SqlDataSource Code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FPConnectionString %>" SelectCommand="HDMonitoringOutstandingToday" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Printscreen of wallboard:
You can try something like this in a timer.
if(GridView1.PageIndex == GridView1.PageCount)
{
GridView1.PageIndex = 0;
}
else
{
GridView.PageIndex = GridView.PageIndex + 1;
}
I cant remember if you would need to add one to the PageIndex or not.
But anyway, the properties you need to work with are PageIndex and PageCount.
GridView.PageIndex
You can change pages by setting the PageIndex, how you do it is up to you see here for some examples: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pageindex.aspx
Im sorry if this has been asked before, but i have searched the forums for quite some time and have not been able to find a solution to my problem.
I am currently developing a web application where users are able to donate bottle deposit. When users have uploaded the bottles they will be shown in the following table:
Snippet of the table
The data is shown in a gridview with a SqlDataSource. The code:
<div class="table-responsive col-8">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WeCANConnectionString %>"
SelectCommand="SELECT [creator], [amount], [details],
[imageurl], [pickup], [available_from] FROM [deposit] ORDER BY [id] DESC"></asp:SqlDataSource>
<asp:GridView id="GridView1" class="table table-hover table-striped table-bordered" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="creator" HeaderText="Uploaded By" SortExpression="creator" />
<asp:BoundField DataField="amount" HeaderText="Amount" SortExpression="amount" />
<asp:BoundField DataField="details" HeaderText="Details" SortExpression="details" />
<asp:BoundField DataField="imageurl" HeaderText="Image" SortExpression="imageurl" />
<asp:BoundField DataField="pickup" HeaderText="Location of the bottles" SortExpression="pickup" />
<asp:BoundField DataField="available_from" HeaderText="Available from" SortExpression="available_from" />
</Columns>
</asp:GridView>
I would like to be able to click on each individual post and create dynamic pages where more data from the database is shown. I read somewhere that i was possible to create these page with the ID of each post but i couldnt find anything that showed how it works.
Be free to ask any follow questions. This is my first post so im not sure if i have covered everything.
Thanks a lot in advance.
One way to do this is with a link in each row. First, you need to add "id" to the SELECT in your DataSource, so the data is accessible at the time the table is bound.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:WeCANConnectionString %>"
SelectCommand="SELECT [id], [creator], [amount], [details], [imageurl],
[pickup], [available_from] FROM [deposit] ORDER BY [id] DESC">
</asp:SqlDataSource>
Then in your GridView, make a template column with a HyperLink, and have the NavigateUrl of that HyperLink open a new page with the ID on the query string. If you don't want the ID to be exposed in the URL, you may need to obfuscate it in some way, or use perhaps a GUID column in the DataBase for each record. As long as you can identify it. Your NavigateUrl can be whichever page you want, but in this example I used DetailPage.aspx.
<asp:GridView id="GridView1" class="table table-hover table-striped table-bordered" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="DetailsLink" runat="server"
NavigateUrl="DetailPage.aspx?id=<%# Eval("id")%>"
Text="Details" Target="_blank">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="creator" HeaderText="Uploaded By" SortExpression="creator" />
<asp:BoundField DataField="amount" HeaderText="Amount" SortExpression="amount" />
<asp:BoundField DataField="details" HeaderText="Details" SortExpression="details" />
<asp:BoundField DataField="imageurl" HeaderText="Image" SortExpression="imageurl" />
<asp:BoundField DataField="pickup" HeaderText="Location of the bottles" SortExpression="pickup" />
<asp:BoundField DataField="available_from" HeaderText="Available from" SortExpression="available_from" />
</Columns>
</asp:GridView>
The <## Eval('id')#> will render the ID for each row as a query string in the URL.
If I understand you well, You can use Template Field
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="text" runat="server" />
</ItemTemplate>
</asp:TemplateField>