Add new row into Header of GridView - c#

I have a problem with adding a new row with several controls like textBoxes into Header of GridView. When I add them into Header in GridView_RowCreated method with ...
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow r = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableCell tc = new TableCell();
.. they always are put into first row, not second. How can I change it? I want my created row put in second Header Row. I tried to do it by myself, Firstly, I modified ShowHeader into false and add both first and second rows programmatically, but it is wrong way, aspecially when grid has no data to show but it is necessary to show header. I found this discussion similar discussion, but intellisence doesn't know about override PrepareControlHierarchy. I tried to search it with Object browser, and found that this is method of GridView, but I couldn't plug it and test. Maybe somebody knows easier solution for this prob.
I need some assist.
UPDATED!!!
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="username" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" onrowcreated="GridView1_RowCreated">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="username" SortExpression="username">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Surname" SortExpression="Surname">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Surname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
UPDATED!!!!
Yes, profs you are right, in my case it is the easiest way to put controls in HeaderTemplate. I forgot about it. But I have a little question, how can I add names of columns without adding additional Labels? Here is my code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="username" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" onrowcreated="GridView1_RowCreated">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField **HeaderText="username"** SortExpression="username">
<HeaderTemplate>
**<asp:Label ID="Label4" runat="server" Text="username"></asp:Label>**<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<HeaderTemplate>
<asp:Label ID="Label4" runat="server" Text="Name"></asp:Label><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Surname" SortExpression="Surname">
<HeaderTemplate>
<asp:Label ID="Label4" runat="server" Text="Surname"></asp:Label><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Surname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
I marked bold strings. If I leave without Label (just with HeaderText="username") it isn't show me any name in header column. Only if I add Labels it shows my names. What is wrong with it?

Dynamically you would have to add a second header row after every has been databound
Example in VB but you should be able to convert it easily enough.
Private Sub GridView1_DataBound(sender As Object, e As EventArgs) Handles GridView1.DataBound
// Add checks for row count.
// create a new row
Dim gvr As New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)
Dim gvc As TableCell
// Create a new empty cell
gvc = New TableCell()
//add a new TextBox to the cell
gvc.Controls.Add(New TextBox())
// Add the cell to the row
gvr.Cells.Add(gvc)
// repeat above as necessary
// Add row to Gridview at index 1 (0 is bound header)
// GridView1.Controls(0) is the Gridview table
GridView1.Controls(0).Controls.AddAt(1, gvr)
End Sub

The simplest solution is to not attempt to do it in the code behind. Instead utilize the HeaderTemplate for your TemplateFields.
Here is one as an example:
<asp:TemplateField HeaderText="username" SortExpression="username">
<HeaderTemplate>
username
<br />
<asp:TextBox ID="username" runat="server" />
</HeaderTemplate>
<ItemTemplate...
</asp:TemplateField>

Related

How can I pass ItemTemplate value to another page?

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.

Adding order buttons to a GridView

I have an example from this gridview:
<asp:GridView ID="GridView3" runat="server">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
<ItemStyle></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("nome")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And I would like to add a button for ordering the data in each column.
Somebody can help me find the best way to do this?
Sorting is built into the GridView control. There is a control Property called AllowSorting, set this to True and you can sort columns by clicking on the header row titles.
There is no need to add extra buttons, even if you want to do some type of special sorting, you'd still Handle the Sorting event and implement your own methods

Can width of a Gridview's column can be changed in percentage in codebehind?

I have a gridview having 5 columns, and last column is visible to some members only.
I want that when the last columngvMessageList.Columns[4] is inivisible, its width in percentage should be given to first columngvMessageList.Columns[0].
Please, let me know, how is it possible.
My GridView is as follow:
<asp:GridView ID="gvMessageList" runat="server" Width="100%" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true"
DataKeyNames="MESSAGE_ID" CellPadding="4" PageSize="51" EmptyDataText="No Records Found." OnSorting="gvMessageList_Sorting"
OnPageIndexChanging="gvMessageList_PageIndexChanging" OnRowDataBound="gvMessageList_RowDataBound" GridLines="None"
CssClass="table table-bordered table-condensed table-hover table-striped">
<Columns>
<asp:TemplateField HeaderText="Subject" HeaderStyle-Width="30%" SortExpression="MESSAGE_SUBJECT" HeaderStyle-BackColor="#D9EDF7"
HeaderStyle-ForeColor="#0088CC">
<ItemTemplate>
<asp:HyperLink ID="hlnkMessageSubject" runat="server" Text='<%# ((System.Data.DataRowView)Container.DataItem)["MESSAGE_SUBJECT"] %>'
NavigateUrl='<%# ((System.Data.DataRowView)Container.DataItem)["MESSAGE_URL"] %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="From" HeaderStyle-Width="14%" SortExpression="MESSAGE_FROM" HeaderStyle-BackColor="#D9EDF7"
HeaderStyle-ForeColor="#0088CC">
<ItemTemplate>
<asp:HyperLink ID="hlinkUser" runat="server" Text='<%#((System.Data.DataRowView)Container.DataItem)["MESSAGE_FROM"] %>'
NavigateUrl='<%#((System.Data.DataRowView)Container.DataItem)["FROM_URL"] %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CREATION_DATE" HeaderText="On" HeaderStyle-Width="15%" HeaderStyle-BackColor="#D9EDF7" HeaderStyle-ForeColor="#0088CC"
SortExpression="CREATION_DATE" />
<asp:TemplateField HeaderText="To" HeaderStyle-Width="21%" HeaderStyle-BackColor="#D9EDF7" HeaderStyle-ForeColor="#0088CC">
<ItemTemplate>
<asp:Label ID="lblTo" runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Broadcast" HeaderStyle-Width="20%" Visible="false" HeaderStyle-BackColor="#D9EDF7" HeaderStyle-ForeColor="#0088CC">
<ItemTemplate>
<asp:Label ID="lblBroadcast" runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In C# Codebehind:
I want increased percentage of first column here:
gvMessageList.Columns[4].HeaderStyle.Dispose();
gvMessageList.Columns[4].Visible = false;
Is it possible by using gvMessageList.Columns[0].HeaderStyle.Width = ?? or something like it.
Give the width property a new unit type. the first parameter is the width you want, and the second is what measurement to use. In the sample below, I'm telling the column to be 50% of the table's width.
gvMessageList.Columns[4].HeaderStyle.Width = New Unit(50, UnitType.Percentage);
You can change the width as follows
GrdDynamic.Columns[4].ItemStyle.Width=Unit.Pixel(500);
GrdDynamic.Columns[4].ItemStyle.Width=New Unit(50, UnitType.Percentage);

Populating a gridview column with an object list

I have a gridview of messages that is populated by an object class. There is a list in the object so that for each message, there is a list of recipients. Problem is, right now the gridview is only displaying "Person[]" for each message. I want it to display as a count of the recipients in the list.
Any help would be appreciated.
HTML
<asp:GridView
ID="grdMsgSent"
runat="server"
CssClass="cellSpacing"
OnRowDataBound="grdMsgSent_RowDataBound"
AllowSorting="True"
EmptyDataText="You have not sent any messages."
AllowPaging="True"
PageSize="6"
OnPageIndexChanging="grdMsgSent_PageIndexChanging"
OnSorting="grdMsgSent_Sorting"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Recipients" HeaderText="Recipients" />
</Columns>
</asp:GridView>
C#
grdMsgSent.DataSource = listSentMsg.List;
grdMsgSent.DataBind();
For example, if you have a data column name called UserID, you can populate in grid view using Eval (DataBinding Expression). So your code will be like this in aspx page,
<asp:TemplateField HeaderText="UserID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblUserID" runat="server" Text='<%# Eval("UserID") %>' />
</ItemTemplate>
</asp:TemplateField>
In C# Code Behind,
DataTable dataTable = new DataTable();
DataColumn dataColumn;
dataColumn = new DataColumn("UserID");
You can use a templatefield:
For array[], do this:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Recipients.Length") %>' />
</ItemTemplate>
</asp:TemplateField>
For List, do this:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Recipients.Count") %>' />
</ItemTemplate>
</asp:TemplateField>

Conditional HyperLink in GridView?

I have a gridview with a hyperlink:
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="False"
CssClass="table table-hover table-striped" GridLines="None" >
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="EmployeName">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Bind("EmployeName") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="EmployeID" Visible="False">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("EmployeID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
However, it should only appear as a hyperlink if the employeeID is that of the logged in employee.
I can do all that, but what I do not know is how to make the hyperlink look like a Label. It's easy to have it not link anywhere, but I do not know how to make it look like a label.
Thanks
I believe if you set Enabled="false" it does. If it does not, then the only way to do that is put both a HyperLink and Label in the cell, and show the link when appropriate, and the label when appropriate, hiding the other one (which can be easily done in RowDataBound event).

Categories