Find column value in Gridview in code behind - asp.net

Here is a gridview from ASPX page (abbreviated):
<asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView_RowCommand" DataKeyNames="order_no" onrowdatabound="Gridview1_RowDataBound" AllowPaging="false" >
<Columns>
<asp:HyperLinkField DataTextField="order_no" HeaderText="Order Number" ItemStyle-Font-Bold="true" /><asp:HyperLinkField />
<asp:BoundField DataField="id_number" HeaderText="Customer ID" ItemStyle-Font-Bold="true" />
</Columns>
</asp:gridview>
The ASPX page has a textbox with a label "Enter Customer ID" and a Button. When user enters a text and clicks the button, I want in the code behind find the value of Order_no.

You might be looking for something like this
<asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView_RowCommand" DataKeyNames="order_no" onrowdatabound="Gridview1_RowDataBound" AllowPaging="false" >
<Columns>
<asp:TemplateField >
<ItemTemplate>
<%# (Eval("order_no") > 0 ? "Something" : "") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id_number" HeaderText="Customer ID" ItemStyle-Font-Bold="true" />
</Columns>
</asp:gridview>

Related

How Change in CodeBehind HeaderImage in GridView

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

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

What is use of Datanavigateurlformatstring of hyperlink in gridview?

How to use Datanavigateurlformatstring of hyperlink in gridview?
I want to display page with the name of product like
http://localhost:55724/Admin/Category/Update/computer when I clicked on computer in gridview, it says resource not found.
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="id" HeaderText="Id" />
<asp:HyperLinkField DataNavigateUrlFields="id"
DataTextField="Productname"
HeaderText="Product"
Target="_blank"
NavigateUrl="Searchdetails.aspx?product={0}"
DataNavigateUrlFormatString="~/Product/Searchdetails.aspx?product={0}" />
</Columns>
</asp:GridView>
How to fix this error.

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>

how to add a tooltip and confirm message box to asp CommandField(image button)

<asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AllowSorting="True"
DataMember="DefaultView"
DataSourceID="SqlDataSource1"...
>
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField ...
<asp:CommandField ButtonType="Image" DeleteText="Delete" ShowDeleteButton="True" DeleteImageUrl="images/deletered1.png"></asp:CommandField>
</Columns>
<FooterStyle BackColor="#507CD1" .../>
</asp:GridView>
I have a grid view with a an <asp:CommandField> in every row but I can't find a prperty to set a tooltip text("delete") or confirm message box (something like "Are you sure?" Yes No)
Try converting your CommandField to a TemplateField, like this:
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="deleteButton" runat="server" CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you sure you want to delete this user?');" />
</ItemTemplate>
</asp:TemplateField>
Then you just need to handle your Command in code-behind.

Resources