How Change in CodeBehind HeaderImage in GridView - asp.net

How to dynamically change the image that is in the header?
The idea is to do it when the grid is created in the "ItemDataBound" event
This is my Grid
<asp:DataGrid ID="dgMain" runat="server" GridLines="Horizontal" AutoGenerateColumns="False" AllowPaging="false">
<Columns>
<asp:BoundColumn DataField="PId" Visible="false" />
<asp:TemplateColumn HeaderStyle-Width="80px" HeaderImageUrl="ico_estadistica_despacho.png">
<ItemTemplate>
<asp:ImageButton ID="btnEst" runat="server" BorderWidth="0" ImageUrl="punto.png" CommandName="EstDesp"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="CotId" Visible="false" />
</Columns>
</asp:DataGrid>
I tried something, but did not work out.
If e.Item.ItemType = ListItemType.Header Then
dgMain2.Columns.Item(1).HeaderImageUrl = "/Images/ico_estadistica_despacho_redi.png"
End If

Related

Grid View Footer doesn't show even though the footer template exists

I have a Grid View. The Data displays correctly. However the footer doesn't show. Can anyone tell me what is wrong ?
Below is the code:-
<asp:GridView ID="grdJLLAlloc" runat="server" AutoGenerateColumns="false" Width="400px"
ShowFooter="True" CellPadding="4" SkinID="OrigBkrAlloc">
<Columns>
<asp:BoundField DataField="Professional" HeaderText="Professional" />
<asp:BoundField DataField="Market" HeaderText="Market" />
<asp:BoundField DataField="BusinessLine" HeaderText="Business Line" />
<asp:BoundField DataField="PSBusinessUnit" HeaderText="Business Unit" />
<asp:TemplateField HeaderText="%">
<HeaderStyle HorizontalAlign="Center" Wrap="True" Width="170px" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblJLLAllocPercentage" runat="server" Text='<%# Eval("Percentage") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblJLLAllocTotalPer" runat="server" Text="" Visible="true"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<HeaderStyle HorizontalAlign="Center" Wrap="True" Width="170px" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblJLLAllocAmt" runat="server" Text='<%# Eval("Amount") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblJLLAllocTotalAmt" runat="server" Text="" Visible="true"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Private Sub LoadJllAllocGrid()
Dim dsJLLCommAlloc As New DataSet("JLLCommissionAlloc")
Try
If m_DealId > 0 Then
dsJLLCommAlloc = oComm.GetCapForceJLLCommissionAlloc(m_DealId)
grdJLLAlloc.DataSource = dsJLLCommAlloc
grdJLLAlloc.DataBind()
End If
Catch ex As Exception
ExceptionPolicy.HandleException(ex, Common.EXCEPTION_POLICY)
End Try
End Sub
Private Sub grdJLLAlloc_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grdJLLAlloc.RowDataBound
If e.Row.RowType = DataControlRowType.Footer Then
Dim lblTotalAmt As Label = DirectCast(e.Row.FindControl("lblJLLAllocTotalAmt"), Label)
lblTotalAmt.Text = "100,000$"
End If
End Sub
It looks like you forgot to bind the grdJLLAlloc_RowDataBound method to the GridView
<asp:GridView ID="grdJLLAlloc" OnRowDataBound="grdJLLAlloc_RowDataBound"

gridview link button redirect to another page

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" EmptyDataText="Sevkiyat Bulunamadı"
class="table table-striped table-bordered table-condensed"
AllowPaging="True"
OnPageIndexChanged="GridView1_PageIndexChanged"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDataBound="RowDataBound"
OnRowCommand="GridView1_RowCommand"
AllowSorting="True"
DataSourceID="SqlDataSource1"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
Teklif İste
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="yetkiliad" ItemStyle-CssClass="yetkiliad" HeaderText="yetkiliad" SortExpression="yetkiliad" />
<asp:BoundField DataField="yetkilisoyad" ItemStyle-CssClass="yetkilisoyad" HeaderText="yetkilisoyad" SortExpression="yetkilisoyad" />
<asp:BoundField DataField="firmaismi" ItemStyle-CssClass="firmaismi" HeaderText="firmaismi" SortExpression="firmaismi" />
<asp:BoundField DataField="telefon" ItemStyle-CssClass="telefon" HeaderText="telefon" SortExpression="telefon" />
<asp:BoundField DataField="adres" ItemStyle-CssClass="adres" HeaderText="adres" SortExpression="adres" />
<asp:BoundField DataField="mail" ItemStyle-CssClass="mail" HeaderText="mail" SortExpression="mail" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="View" ID="lnkView" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle VerticalAlign="Middle" />
</asp:GridView>
I have code like this. I cant find how to redirect when click view button on each row. I mean when i clicked second row's view it shoudl be redirected related link with second row. Could you help me about it
No need to create a LinkButton inside the asp:TemplateField. Just use an asp:HyperLinkField like this
<asp:HyperLinkField Text="View"
DataNavigateUrlFields="firmaname"
DataNavigateUrlFormatString="~/{0}.aspx" />
<asp:LinkButton ID="lnkView" runat="server" OnClick="lnkView_Click" CommandArgument='<%#Eval("Id") %>View</asp:LinkButton>
protected void lnkView_Click(object sender, EventArgs e)
{
int Id = (sender as LinkButton).CommandArgument;
Response.Redirect("Foo.aspx?Id=" + Id);
}

Adding Drop down inside gridview header

I am new to ASP.net and trying to add dropdownlist and search text boxes under my header fields in Gridview control.
<asp:GridView ID="EmpGridView" runat="server" AutoGenerateColumns="false"
DataKeyNames="EMPLOYEEID"
AllowSorting="True" AllowPaging="true" PageSize="50"
OnPageIndexChanging="EmpGridView_PageIndexChanging" style="margin-right: 52px" OnSelectedIndexChanged="EmpGridView_SelectedIndexChanged"
>
<Columns>
<asp:BoundField DataField="EMPLOYEEID"
HeaderText="Employee ID" ReadOnly="true"
SortExpression="EMPLOYEEID" />
<asp:BoundField DataField="PERSONNAME"
HeaderText="Person Name" ReadOnly="true"
SortExpression="PERSONNAME" />
<asp:BoundField DataField="DIVISIONNAME"
HeaderText="Division Name" ReadOnly="true"
SortExpression="DIVISIONNAME" />
<asp:BoundField DataField="DESIGNATION"
HeaderText="Designation" ReadOnly="true"
SortExpression="DESIGNATION"
/>
<asp:BoundField DataField="CNIC"
HeaderText="CNIC" ReadOnly="true"
SortExpression="CNIC" />
</Columns>
</asp:GridView>
I want following
EMPLOYEEID PERSONNAME DIVISIONNAME <----HeaderText
TextBox control TextBox Control DropDownlist control <------aspcontrols
..data ..data ..data <------ rest is db
..data ..data ..data
Meaning I want my labels there too with my asp.net controls
How should I do it?
So far I tried following but could not work out where to place it? since if I add it separately then there are rows of each control which I don't want and boundfield tag does not permit it in it.
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="searchBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
You need to use HeaderTemplate of the TemplateField for placing the controls on the header
<asp:TemplateField SortExpression="PERSONNAME">
<HeaderTemplate>
<asp:Literal runat="server">Person Name</asp:Literal>
<asp:TextBox runat="server" ID="searchBox"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Literal runat="server" Text='<%# Bind("PERSONNAME") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>

Accessing TemplateField controls value of gridview inside button click handler in asp.net

After clicking the edit linkbutton of my gridview, I show the data on different text boxes which are not inside the gridview. I have a "Reset" button which i want to use to get back to the original values. But I am having problem to access those gridview data inside the button click handler and reset it.I tried using DirectCast() but its showing System.NullReferenceException.
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lblEdit" runat="server" CausesValidation="false" CommandName="editRecord" Text="EDIT" CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id" Visible="False">
<ItemTemplate>
<asp:Label ID="lblRecordID" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="HANGER">
<ItemTemplate>
<asp:Label ID="lblHANGER" runat="server" Text='<%# Bind("HANGER") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns></asp:GridView>
The backend vb.net code is-
Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnReset.Click
Dim vID As Label = DirectCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label)
Dim vHanger As Label = DirectCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label)
txtID.Text.Text = vID.Text()
ddlHanger.SelectedValue = vHanger.Text 'dropdown list that's why selectedValue used
End Sub
I have copied the portion of the code cause the gridview has lot more rows. I would appreciate if anyone please show me a solution.Thanks in advance.
First remove the following:
<ItemTemplate>
<asp:LinkButton ID="lblEdit" runat="server" CausesValidation="false"
CommandName="editRecord" Text="EDIT"
CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton>
</ItemTemplate>
And add:
<asp:CommandField ButtonType="Button" ShowSelectButton="True" SelectText="EDIT" />
That markup will allow your GridView to have an EDIT button which will switch the current selected row index correctly. System.NullReferenceException error you are recieving could be because the GridView3.SelectedRow is NULL/EMPTY, which also means GridView3 currently has NO selected index.
To make sure that GrieView3 indeed selected a ROW, you can add: the following right AFTER the
<SelectedRowStyle BackColor="Black" BorderColor="White" BorderStyle="Dotted"
BorderWidth="3px" ForeColor="White" />
right after
</Columns>
So your final GridView3 markup should look like:
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
<Columns>
<asp:CommandField ButtonType="Button" ShowSelectButton="True"
SelectText="EDIT" />
<asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id" Visible="False">
<ItemTemplate>
<asp:Label ID="lblRecordID" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="HANGER">
<ItemTemplate>
<asp:Label ID="lblHANGER" runat="server" Text='<%# Bind("HANGER") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="Black" BorderColor="White" BorderStyle="Dotted"
BorderWidth="3px" ForeColor="White" />
</asp:GridView>
Then you can also use TryCast too, like so:
Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnReset.Click
Dim vID As String = TryCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label).Text
Dim vHanger As String = TryCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label).Text
txtID.Text.Text = vID
ddlHanger.SelectedValue = vHanger
End Sub

Add a column to a datagrid

I am trying to add a column with dropdownlist to a DataGrid
Here is the code for the DataGrid binding datasource
List<CPDEmployee> employeelist = (List<CPDEmployee>)Cache["EmployeeList"];
unverifiedlist.DataSource = employeelist;
unverifiedlist.DataBind();
unverifiedlist.AllowPaging = true;
unverifiedlist.PageSize = 10;
In the page.aspx code is like this
<asp:DataGrid ID="unverifiedlist" runat="server" AllowPaging="true" PageSize="10" OnPageIndexChanged="unverifiedlist_PageIndexChanged">
<Columns>
<asp:BoundColumn HeaderText="Surname" DataField="Surname" ReadOnly="true">
</asp:BoundColumn>
</Columns>
<Columns>
<asp:TemplateColumn HeaderText="Options" >
<ItemTemplate>
<asp:DropDownList ID="options" runat="server">
<asp:ListItem Value="1">Verified</asp:ListItem>
<asp:ListItem Value="0">Rejected</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText ="Reason">
<ItemTemplate>
<asp:TextBox ID="reason" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
The result is there are two "Surname" fields in the datagrid. The reason I added "Surname" from page side is that i can't set the order of the columns (the dropdownlist should be at last).
Does anyone know how to solve this problem? thanks very much
You have to turn off AutoGenerateColumns and specify all the bound fields.

Resources