how to view specific row on Gridview? - c#

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;
}
}

Related

How to allow GridView sort and display text as link

I want to be able to allow the user to sort through by clicking on each header of the following GridView
<asp:GridView OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="There is no data to display"></asp:GridView>
I am populating the above from a SQL query:
string query = #"SELECT CT.ATTR2739 'Task Name'
,UG.USERGROUPNAME 'Department'
,CT.ATTR2812 'Status'
,CT.ATTR2752 'Due Date'
,'http://dvmag/appnet/workview/objectPop.aspx?objectid=' + CAST(CT.OBJECTID AS VARCHAR) + '&classid=1224' 'Link'
FROM HSI.RMOBJECTINSTANCE1224 CT LEFT JOIN HSI.USERGROUP UG on CT.FK2743 = UG.USERGROUPNUM
WHERE CT.ACTIVESTATUS = 0";
using (SqlConnection conn = new SqlConnection(connString))
{
try
{
SqlCommand cmd = new SqlCommand(query, conn);
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(query, conn);
// this will query your database and return the result to your datatable
da.Fill(taskData);
//conn.Close();
yourTasksGV.DataSource = taskData;
yourTasksGV.DataBind();
}
catch (Exception ex)
{
string error = ex.Message;
}
}
Which displays the following:
How can I make it so that the Task Name, Department, Status, Due Date, and Link are clickable which will sort ASC/DESC?
How can I make the Link rows all links with the display to be the Task Name? (ex: Test Event 1)
I changed my GridView to this:
<asp:GridView OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="There is no data to display">
<Columns>
<asp:BoundField DataField="Text" HeaderText="Task Name" SortExpression="TaskName" />
<asp:BoundField DataField="Text" HeaderText="Department" SortExpression="DepartmentName" />
<asp:BoundField DataField="Text" HeaderText="Status" SortExpression="TheStatus" />
<asp:BoundField DataField="Text" HeaderText="Due Date" SortExpression="DueDate" />
<asp:BoundField DataField="Link" HeaderText="Complete Task" SortExpression="CompleteTask" />
</Columns>
</asp:GridView>
And this is what showed up:
Add columns explicitly as shown here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns(v=vs.110).aspx
Your DataTextFields must align with the column names - not "Text" or "Link" (unless Text and Link are valid column names).
This will allow you to specify the column type as a hyperlink. Rather than including your full link in the query, use the DataNavigateUrlFormatString in the HyperLinkField. The HyperLinkField allows you to define the DataNavigateUrlField and the DataTextField separately.
See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield(v=vs.110).aspx
To make columns sortable, be sure to set the SortExpression on the column.
When defining columns in this way, you must also set AutoGenerateColumns to false on the GridView.
<asp:GridView OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="There is no data to display" AutoGenerateColumns ="False">
<Columns>
<asp:BoundField DataField="Task Name" HeaderText="Task Name" SortExpression="TaskName" />
<asp:BoundField DataField="Department" HeaderText="Department" SortExpression="DepartmentName" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="TheStatus" />
<asp:BoundField DataField="Due Date" HeaderText="Due Date" SortExpression="DueDate" />
<asp:HyperLinkField DataNavigateUrlFields="Link" DataTextField="Task Name" DataNavigateUrlFormatString="http://...objectid={0}&classid=12240" HeaderText="Complete Task" SortExpression="CompleteTask" />
</Columns>
</asp:GridView>
Then, change the SELECT portion of your query to something like this:
SELECT
CT.ATTR2739 'Task Name'
,UG.USERGROUPNAME 'Department'
,CT.ATTR2812 'Status'
,CT.ATTR2752 'Due Date'
,CT.OBJECTID 'Link'
I'd suggest converting them to template fields. Here is an example of how one would look.
<asp:TemplateField HeaderText="Link" SortExpression="Link">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Link") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Bind("Link") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
If you won't be editing the information, you can remove the EditItemTemplate, and that will clean it up a bit. Hope this helps!
EDIT:
To convert them to templatefields, you will want to click on the smart tag on your gridview when you are in design view. Once you do that, click edit columns, select the columns you want to convert, then select the convert to templatefield option in the lower right hand corner of that page. Once you do that, if you want to use a link button you will have to change the ones you want from a label as when it converts them, textboxes are default for edit and labels are default for item.

Why doesn't ASP.net GridView update MS ACCESS record?

I have:
<asp:GridView ID="ClassesGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="CourseNumber" DataSourceID="AccessClasses"
AutoGenerateEditButton="True" >
<Columns>
<asp:BoundField DataField="CourseNumber" HeaderText="CourseNumber" ReadOnly="True" SortExpression="CourseNumber" />
<asp:BoundField DataField="Teacher1" HeaderText="Teacher1" SortExpression="Teacher1" />
<asp:BoundField DataField="T1PhoneNumber" HeaderText="T1PhoneNumber" SortExpression="T1PhoneNumber" />
<asp:BoundField DataField="T1Email" HeaderText="T1Email" SortExpression="T1Email" />
<asp:BoundField DataField="Teacher2" HeaderText="Teacher2" SortExpression="Teacher2" />
<asp:BoundField DataField="T2PhoneNumber" HeaderText="T2PhoneNumber" SortExpression="T2PhoneNumber" />
<asp:BoundField DataField="T2Email" HeaderText="T2Email" SortExpression="T2Email" />
<asp:BoundField DataField="OrderToVisit" HeaderText="OrderToVisit" SortExpression="OrderToVisit" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessClasses" runat="server" DataFile="~/App_Data/SundaySchool.mdb"
SelectCommand="SELECT [CourseNumber], [Teacher1], [T1PhoneNumber], [T1Email], [Teacher2], [T2PhoneNumber], [T2Email], [OrderToVisit] FROM [Classes]"
UpdateCommand="UPDATE Classes SET Teacher1 = #Teacher1 WHERE CourseNumber = #CourseNumber">
</asp:AccessDataSource>
For the life of me I can't figure out why this won't update. The 'Edit' record works, but after changing the record and hitting 'Update' nothing happens. Everything reverts back.
EDIT:
Something weird is happening here. If I set a value in the 'Teacher2' column, that will then populate to the 'Teacher1' column. The CourseNumber column also isn't what it should be which is why WHERE CourseNumber = #CourseNumber is not working.
You have to change the UpdateCommand, notice ? instead of #:
<asp:AccessDataSource ID="AccessClasses" runat="server" DataFile="~/App_Data/SundaySchool.mdb"
SelectCommand="SELECT [CourseNumber], [Teacher1], [T1PhoneNumber], [T1Email], [Teacher2], [T2PhoneNumber], [T2Email], [OrderToVisit] FROM [Classes]"
UpdateCommand="UPDATE Classes SET Teacher1 = ? WHERE CourseNumber = #CourseNumber">
</asp:AccessDataSource>
Here's more info about AccessDataSource: MSDN.
I figured this out. The UpdateCommand MUST follow the SelectCommand exactly with exception to the Primary Key, CourseNumber. EVERYTHING in the select must be in the update in the correct. Once I did that, everything worked great!!

AutoGenerateInsertButton in Gridview

I have the following GridView
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
OnRowCommand="GridView1_RowCommand" DataKeyNames="Chart_Id"
AutoGenerateColumns="False" EnableModelValidation="True" >
<Columns>
<asp:CommandField ShowEditButton="False" ShowDeleteButton="False" ShowInsertButton="False" />
<asp:BoundField DataField="Week" HeaderText="Week" SortExpression="Week" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" ItemStyle-Wrap="False" />
<asp:BoundField DataField="Host" HeaderText="Host" SortExpression="Host" />
<asp:BoundField DataField="Topic_1" HeaderText="Topic 1" SortExpression="Topic_1" />
<asp:BoundField DataField="Topic_2" HeaderText="Topic 2" SortExpression="Topic_2"
HeaderStyle-Wrap="False" />
<asp:BoundField DataField="Topic_3" HeaderText="Topic 3" SortExpression="Topic_3" />
<asp:BoundField DataField="Topic_4" HeaderText="Topic 4" SortExpression="Topic_4" />
</Columns>
</asp:GridView>
By default, I have the edit/insert/cancel buttons set to false.
Then in the code behind, I want to be able to set these to true during certain conditions.
string theUser = Helpers.GetUser();
string admin = "adminName";
if (theUser == admin) {
// Set the buttons to true
}
I've been looking for ways to do this, and someone suggested to use the AutoGenerate properties, and then enable them like so:
GridView1.AutoGenerateEditButton = true;
GridView1.AutoGenerateDeleteButton = true;
GridView1.AutoGenerateInsertButton = true; // This one throws an error
Only problem is, AutogenerateInsertButton does not seem to exist, in the main ASPX page or in the code behind.
Can anyone suggest some ways for me to access these properties and set them to true?
Thank you.
Why do you think that a GridView should have an AutoGenerateInsertButton property?
A GridView is a list of GridViewRows, where each row represents a record/element/item which can be edited or deleted. But it doesn't make sense to have a insert-button for each record because it already exists.
You could follow this tutorial which shows how to use the footer-row of the GridView to insert a new record.
The property AutoGenerateInsertButton exists on the DetailsView control. Whoever designed the control probably figured that you don't need an insert button for each row in the grid, since each would essentially do the same thing.
So, maybe you could display an empty DetailsView at the bottom of the grid, or just create your own insert command using a regular Button.

ASP.NET Gridview Buttons

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

Is there a way to make asp.net Boundfield a link?

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>

Categories

Resources