I have following field in a grid view:
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("JobEstimation").ToString() == "1" ? "Yes" : "No" %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Now how do I enable sorting in this filed? "SortExpression="???"".
So when the user click the sort it should sort base on alphabet order.
As stated in comments, it would be better to implement that conversion within the Database, but pertinent to you question, the answer would depend on the actual content of that field. As a simple solution, you may try the following code snippet:
<asp:TemplateField HeaderText="Job Est" SortExpression="JobEstimation">
<ItemTemplate>....</ItemTemplate>
</asp:TemplateField>
Alternatively, you may add the Command/Link Button to the HeaderTemplate:
<asp:TemplateField SortExpression="JobEstimation">
<HeaderTemplate>
<asp:LinkButton ID="sortJobEst" runat="server" Text="Job Est" CommandName="Sort" CommandArgument="JobEst" />
</HeaderTemplate>
</asp:TemplateField>
and hook it to the GridView RowCommand event (re: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand(v=vs.110).aspx):
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Sort"))
{
// add your code to sort
e.CommandArgument.ToString()...
BindGridView();
}
}
Hope this may help.
Related
Greetings fellow developers. I am new to ASP.NET C# so pardon me if there are areas that I missed out/
Current in my project, I have this Gridview - where I used "TemplateField" for almost every data field. My issue is, I do not know how can I pass the value in to another page. I tried using SESSION but unfortunately, it is not working. I also tried using BOUNDFIELD, it works but it does not fulfil my project requirement as the default setting for BOUNDFIELD is textbox, I want it to be a DROPDOWN LIST control instead. Any kind advice would be much appreciated. Attached below is my codes.
WebForm1.aspx
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="Gridview1_PageIndexChanging" OnRowCancelingEdit="Gridview1_RowCancelingEdit" OnRowCommand="Gridview1_RowCommand" OnRowDeleting="Gridview1_RowDeleting" OnRowEditing="Gridview1_RowEditing" OnRowUpdating="Gridview1_RowUpdating" ShowFooter="True" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" ShowHeaderWhenEmpty="True" Width="100%" CssClass="table table-responsive table-bordered" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged" DataKeyNames="CID">
<Columns>
<asp:TemplateField HeaderText="Transaction ID">
<EditItemTemplate>
<asp:Label ID="lbleditid" runat="server" Text='<%# Bind("CID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Bind("CID") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="hidden-xs" />
<ItemStyle CssClass="hidden-xs" />
</asp:TemplateField>
<asp:TemplateField HeaderText="CCID">
<EditItemTemplate>
<asp:Label ID="lblccid2" runat="server"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblccid" runat="server" Text='<%# Bind("CCID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" SortExpression="Category">
<EditItemTemplate>
<%--<asp:TextBox ID="txtBox_Cat" runat="server" Text='<%# Bind("Category") %>'></asp:TextBox>--%>
<asp:DropDownList ID="ddlCategory" runat="server" >
<asp:ListItem Value="--Select--">--Select--</asp:ListItem>
<asp:ListItem Value="Transportation">Transportation</asp:ListItem>
<asp:ListItem Value="Children">Children </asp:ListItem>
<asp:ListItem Value="Food">Food</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlCategory1" runat="server">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>Transportation</asp:ListItem>
<asp:ListItem>Children </asp:ListItem>
<asp:ListItem>Food </asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Edited">
<EditItemTemplate>
<asp:TextBox ID="txtDateTime1" runat="server" Text='<%# Bind("Last_Edited") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDateTime" runat="server" ToolTip="DD/MM/YYYY"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblDateTime" runat="server" Text='<%# Bind("Last_Edited") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="true" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="PassData">Select</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
WebForm1.aspx.cs
protected void PassData(object sender, EventArgs e)
{
GridViewRow gr = ((sender as LinkButton).NamingContainer as GridViewRow);
Session["Category"] = gr.Cells[1].Text.Trim();
Response.Redirect("AfterUserMthlyExpenses.aspx");
}
WebForm2.aspx
<asp:Label ID="lblPassCategory" runat="server" ></asp:Label>
WebForm2.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Passing "Category" from previous page
lblPassCategory.Text = Session["Category"].ToString();
}
}
Well, really one should hold off on the passing of the value to the next page until such time you have a working simple answer to select/get the given row that you clicked on right?
Be it a repeater, grid view, list view (details view) etc.?
In asp.net they ALL FOLLOW the same process. You want to have that button click set/move/change to the correct given row that you clicked on. Until you have that “movement” or selection of the row occur, then you will fail at attempting to grab values from that row.
So, looking at this, you have a link button (it could be a asp.net button if want – don’t matter), you need that button to trigger/set/move/cause the row you are working on to change FIRST and BEFORE you attempt to grab data/values from that given row.
The WAY you do this is to add a special command. As noted, this works for list view/grivdview/repeater and MANY more data bound controls.
so what you learn here can apply to just about ANY data aware control (that repeats data).
So, add this to the one link button in the item template:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server"
CommandName = "MyJump"
CommandArgument = '<%# Eval("Category") %>'
>Select</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
So, the INSTANT you add CommandName="Select", then this will cause TWO events of the grid to fire.
OnRowComand:
The row command event will fire. But the row HAS NOT YET changed!
However, since we use CommandArgument and pass "Catagory", then we of course can use the rowcomamnd event, and pick up the CommandArgument value.
So, you can have this code in row command event:
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
Debug.Print("row command = " & e.CommandName)
Debug.Print("row arg = " & e.CommandArgument)
If e.CommandName = "MySelect" Then
' do whatever
End If
End Sub
Note how we don't need (you can remove) the on-click event for that button - you use the rowcommand event stub, and pick up the custom command name you passed (MySelect).
You can ALSO trigger the selected row event to fire. This would allow you to use/keep/have your EXISTING code click stub for your button.
However you now MUST change the CommandName from "your custom" name to either;
Select (move the grid pointer)
Edit - trigger edit event
Delete - trigger delete event
However, in your button click (as you have now).
You could try this:
Dim btn As Button = sender
Dim gvRow As GridViewRow = btn.Parent.Parent
Debug.Print("btn row sel = " & gvRow.RowIndex)
Debug.Print("btn argument = " & btn.CommandArgument)
So you can try btn.Parent (that will be the grid cell, and parent again will return the ONE row as you have. From that, you can grab any value out of that row.
eg:
dim myLable as label
myLabel = gvRow.FindControl("Label1")
debug.print myLabel.Text (should return catagory).
Probably most easy to just add a CommandArgument, and pick it up from sender.
Now that you first and foremost verify that you have the correct value, then you can shove that value into session. in fact you can even shove in the whole gvRow into session, and thus pass all of the values of that row to the page you jump to.
I am having trouble retrieving the new value that is entered in a textbox template field in my GridView.
Here is my markup:
<asp:TemplateField HeaderText="username" SortExpression="username">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUserName" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
And here is how I'm trying to retrieve the new value, inside the RowCommand event handler of my GridView:
string userName = ((TextBox)grdUserList.Rows[rowIndex].FindControl("txtUserName")).Text;
I get the old value instead of the newly typed value when I execute this code.
Does anyone know what I am missing? Thanks in advance.
I just found out the solution for my problem. I searched and found out that the GridView is being refreshed before the retrieving process begins because I was rebinding the GridView on the Page_Load method. I fixed the problem by not rebinding the gridview when it is a post back (or at least not before I have made the changes) using the IsPostback method. Thanks for everyone's reply :)
You are retrieving new value in wrong GridView event. You have to add OnRowUpdating="grdUserList_RowUpdating" event in your GridView control and then retrieve new TextBox value.
OnRowUpdating event in code-behind:
protected void grdUserList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string userName = ((TextBox)grdUserList.Rows[e.RowIndex].FindControl("txtUserName")).Text;
// Write your update query and logic over here.
}
You can take a reference from here for additional knowledge.
Please let me know if you have any questions.
use this code in gridview
<Columns>
<asp:TemplateField HeaderText="SrNo">
<EditItemTemplate>
<asp:TextBox ID="txtsrno" runat="server" Text='<%#Eval("SrNo") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblsrno" runat="server" Text='<%#Eval("SrNo") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
I have a GridView which is pretty functioning except for one thing: when I click the Edit link button, it appears a list of textboxes to edit the values as I want. But, when I click the update button and do ((TextBox)row.Cells[2].Controls[0]).Text (for example), it returns me the value that was there before the I edited the textbox!
Any idea of how to access the new texts of the textboxes?
Hey You have to do on Gridview_RowUpdating command like that
protected void gvSalaryDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
((TextBox)gvSalaryDetails.Rows[e.RowIndex].FindControl("txtgvSalaryHeadValue")).Text;
}
and Aspx page shoul follow like that.
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<div class="control">
<asp:Label ID="lblgvSalaryHeadValue" runat="server" Text='<%# ((SalaryDetails)Container.DataItem).HeadValue %>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtgvSalaryHeadValue" runat="server" Text='<%# ((SalaryDetails)Container.DataItem).HeadValue %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Hope it Helps you..
Hi all I am having a template field as follows with an itemtemplate
<asp:TemplateField HeaderText="Edit/Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" OnClick=lnkEdit_Click"> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Generally instead of Click event we use to write CommandName="Edit" and on OnRowEditing event we will set gridview row to edit mode with the following code
protected void grdDemo_RowEditing(object sender, GridViewEditEventArgs e)
{
grdDemo.EditIndex = e.NewEditIndex;
bindGrid();
}
Instead of this I would to set gridview row to edit mode on link button click, how can we do that any ideas please
There are couple of other option available since you wish to ignore the commandname :)
Click anywhere to activate edit mode in gridview
Activate Edit mode based on ID - Datakey
Set the EditIndex property to the appropriate row and then ReBind the GridVIew again to it's DataSource.
protected void btnEdit_Click(object sender, EventArgs e)
{
GridView1.EditIndex = 1;
}
Google/Bing for more..
You can use edit item template as follows
the following is the sample aspx code
<ItemTemplate>
<asp:LinkButton ID="lblSubject" Width="100%" Height="100%" CommandName="Edit" ForeColor="Black" runat="server" Text='<%#Bind("Subject") %>'>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lblSubject" runat="server" Text='<%#Bind("Subject") %>'>
</asp:TextBox>
</EditItemTemplate>
I need some help again.
I have a GridView with CheckBoxes in first column. If a user checks multiple CheckBoxes and clicks a button I need to display selected rows in edit mode. How do I do that? Any tips?
Some sample code, how I imagine it.
Here is my Gridview MyGV:
<asp:GridView ID="MyGV" runat="server" AutoGenerateColumns="False" DataKeyNames="ID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ChkBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:TemplateField HeaderText="Column1">
<EditItemTemplate>
<asp:TextBox ID="tbColumn1" runat="server" Text='<%# Bind("column1") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labColumn1" runat="server" Text='<%# Bind("column1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column2">
<EditItemTemplate>
<asp:TextBox ID="tbColumn2" runat="server" Text='<%# Bind("column2") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labColumn2" runat="server" Text='<%# Bind("column2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnEdit" runat="server" Text="EDIT" OnClick="btnEdit_Click" />
In code behind I bind that GridView to display data from SQL table (no problem there). Now I need it to enter selected rows into edit mode (ONLY selected rows) when I click the EDIT button.
protected void btnEdit_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in MyGV.Rows)
{
CheckBox checkbox = (CheckBox)row.FindControl("ChkBox");
if (checkbox.Checked)
{
// ENTER EDIT MODE - Help needed here!! :)
}
}
BindGridView();
}
In a GridView only one row can be in true edit mode at the same time. See question below for a possible workaround:
Put multiple rows of a gridview into edit mode