I Have this GridView :
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
AllowPaging="True" PageSize="20" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
And this id the SqlDataSource1 :
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connectionString %>"
ProviderName="<%$ ConnectionStrings:connectionString.ProviderName %>"
SelectCommand="SELECT count(city),city FROM users GROUP by city ORDER BY count(city) DESC;" ></asp:SqlDataSource>
And in the GridView the headers for the columns is count(city),city.
How should i change the columns names?
Change your SQL to:
SelectCommand = "SELECT count(city) As CityCount, city FROM users ..."
Therefore renaming the header to CityCount using ALIAS
For more flexibility, disable autogeneration of column from the Gridview and specify it manually: AutoGenerateColumns="False"
<asp:GridView AutoGenerateColumns="False" ID="GridView1" runat="server"
DataSourceID="SqlDataSource1" AllowPaging="True" PageSize="20"
CellPadding="4" ForeColor="#333333" GridLines="None">
<asp:BoundField DataField="CityCount" HeaderText="Number of Cities" />
</asp:GridView>
You can change your Sql select query using alias as codingbiz mention in his answer
OR
Code: Dynamically set headertext
GridView1.Columns[ColumnIndex].HeaderText = "Header text";
try this
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="True"
PageSize="20" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false"><%--add attribute AutoGenerateColumns="false" --%>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<Columns>
<asp:BoundField HeaderText="Total" DataField="City_Total" />
<asp:BoundField HeaderText="City" DataField="City" /> <%--Header Text => Whatever you want to display as name ;DataField=> name of field--%>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connectionString %>"
ProviderName="<%$ ConnectionStrings:connectionString.ProviderName %>" SelectCommand="SELECT count(city) as City_Total,city FROM users GROUP by city ORDER BY count(city) DESC;">
</asp:SqlDataSource>
Change your sql query to the following
SELECT count(city) As CityCount, city FROM users GROUP by city ORDER BY count(city) DESC;"
and then within the grid view add bound fields with the header text set to whatever you want
<Columns>
<asp:BoundField DataField="CityCount" HeaderText="City Count"
SortExpression="CityCount" />
<asp:BoundField DataField="city" HeaderText="City "
SortExpression="city" />
</Columns>
You should change the query like this
SELECT count(city) AS CityCount, city FROM users GROUP by city ORDER BY count(city) DESC;
You Can Change Your SQL
CMD="SELECT Count(City) As Count_City, etc,... FROM [Tbl_Name]
From where I see it, You could do it 2 ways :
1) Update your SQL query :
SELECT count(city) as CityCount,city as CityName FROM users GROUP by city ORDER BY count(city) DESC;
2) Use the ´TemplateField´ or BoundField functionality of gridview (and then defining yourself your columns) :
<asp:TemplateField HeaderText="Discount" HeaderText="City Count" >
<ItemTemplate>
<asp:Literal ID="CityCount" runat="server" Text='<%# Eval("CityCount").ToString() %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
In that case don't forget to add the following attribute to your gridview, cause else you'll have duplicate column... :
AutoGenerateColumns="false"
Related
I am doing a simple project on Visual Studio 2013 about a book library.
I have a database from which I will get the information for the GridView's.
The purpose of this page is to display the list of authors and their respective titles.
So, basically, I have a GridView which will display the author's names and IDs.
I added a column to the end and edited it as a template so I could include another GridView - this one will contain the book titles for the respective author in the first GridView.
The first GridView is connected to a first ObjectDataSource which will call a function GetData() (which will return the right table to be shown on the GridView).
The second GridView is connected to a second ObjectDataSource which will call a function GetTitlesByAuthor(). But, this function receives an argument: the au_id (author_ID).
My problem is: How can I pass the parameter au_id depending on the value of the au_id displayed on the first GridView rows?
EDIT: To be more clear:
I have a GridView with 3 columns (au_id, au_name, titles). I set the titles field to be a template so I could insert a gridview in the ItemTemplate section. That GridView will contain only a collumn with all the titles from a specific au_id (depending on the outter GridView row's au_id value). My problem is obtaining that value to send to the second ObjectDataSource containing a list of the titles.
So my final table would be something like:
au_id au_name titles
--------------------------
1 Gary A Book Title
Another Book I Wrote
2 Sarah Cooking book
Tech Book
... and so on ...
This is my current code:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="au_id" DataSourceID="ObjectDataSource1">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="au_id" HeaderText="Author ID" ReadOnly="True" SortExpression="au_id" />
<asp:BoundField DataField="au_lname" HeaderText="LastName" SortExpression="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="FirstName" SortExpression="au_fname" />
<asp:TemplateField HeaderText="Titles">
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="ObjectDataSource2" ForeColor="#333333" GridLines="None" ShowHeader="False" DataKeyNames="au_id">
<AlternatingRowStyle BackColor="Transparent" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/bullet.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="Transparent" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetTitlesByAuthor" TypeName="MyStore.DataSet1TableAdapters.AuthorTitlesTableAdapter">
<SelectParameters>
<asp:Parameter DefaultValue="409-56-7008" Name="author_ID" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="MyStore.DataSet1TableAdapters.AuthorsTableAdapter"></asp:ObjectDataSource>
ohh it's then nested gridview.I havent done work on objectdatasource but as I understand you have a main grid where you want to pass author id in child grid whuch shows books.
The way I would have done it..
protected void GVAuthor_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblauthid= (Label)e.Row.FindControl("lblCustomerID");//I dont know what is your grid source aspx so assuming it as label.
GridView GvBook = (GridView)e.Row.FindControl("GvBook");
bindChildGridview(Convert.ToInt32(lblauthid.Text), GvBook); //Bind the child gridview here ..
}
}
private void bindChildGridview(int authorId, GridView ChildGridview)
{
try
{
Get datasource based on authorId
ChildGridview.DataSource = <<Your Datasource>>; // Set DataSource Here
ChildGridview.DataBind();
}
catch (Exception) { }
}
The Select parameters for ObjectDataSources are stored in the InputParameters array. So, the value of the author id in the row needs to be added to this array.
Try this:
protected void GVAuthor_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var authorId = e.Row.DataItem("au_id");
var ods = e.Row.FindControl("ObjectDataSource2") as ObjectDataSource;
ods.InputParamenters["author_ID"] = authorId;
}
}
You also may need to remove the DataSource attribute in GridView2 in the aspx page, set it dynamically in the rowDataBound event and then call DataBind as #coder001 does.
Here is nice approach described in msdn you can take a look objectdatasouce parameter.
I didn't want to change c# code.
So, to accomplish my goal I added an invisible Label next to the nested GridView, bound it to au_id and then configured the ObjectDataSource to get the parameter from the Label.
The resulting code is below:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="au_id" DataSourceID="ObjectDataSource1" AllowPaging="True">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="au_id" HeaderText="Author ID" ReadOnly="True" SortExpression="au_id" />
<asp:BoundField DataField="au_lname" HeaderText="LastName" SortExpression="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="FirstName" SortExpression="au_fname" />
<asp:TemplateField HeaderText="Titles">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("au_id", "{0}") %>' Visible="False"></asp:Label>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="ObjectDataSource2" ForeColor="#333333" GridLines="None" ShowHeader="False" BackColor="Transparent" DataKeyNames="au_id">
<AlternatingRowStyle BackColor="Transparent" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/bullet.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" ConvertEmptyStringToNull="False" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="Transparent" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetTitlesByAuthor" TypeName="MyStore.DataSet1TableAdapters.AuthorTitlesTableAdapter">
<SelectParameters>
<asp:ControlParameter ControlID="Label1" Name="au_id" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="MyStore.DataSet1TableAdapters.AuthorsTableAdapter"></asp:ObjectDataSource>
How to make any column hyperlinked (except first one) in dynamic gridview , where that hyperlink column is linked to another gridview.
This dynamic table shows two type of output as mentioned below:
View 1:
Start Bus_no Change_at Change_Bus_no Destination Means
Desu Office 715 Hanuman Mandir 781 Subroto Park DTC
Desu Office 715 Palam Airport 764 Subroto Park DTC
Desu Office 715 Palam Airport 781 Subroto Park DTC
View 2:
Start bus_no Destination Means
Subroto Park 764 Nehru Place Terminal DTC
In first view I want to make column 2nd (Bus_no) and 4th(Changed_Bus_no) and in second view I want to make column 2nd as hyperlink column which will be linked to another grid view table.
Gridview Code is given below:
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
GridLines="Vertical" ForeColor="Black">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
please help. Thanx in advance.
You can use Template Field for your requirement.
Try like this,
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
GridLines="Vertical" ForeColor="Black">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
<Columns>
<asp:TemplateField HeaderText="First Column">
<ItemTemplate>
<asp:Label ID="lblthird0" runat="server" Text="items" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Second Column">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="#Gridview2"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can do like this for all your columns.
I've got a GridView who's DataSource SELECTs based on the SelectedValue of another GridView.
On the GridView with the SelectedValue (DataKeyName), I need to execute the Select command when the grid loads, rather that using a "Select" button.
I've tried using the SelectedIndex attribute of the GridView but this wont do it for me.
I've also looked at various methods and events but I cant seem to find a good way of doing this.
In the code below, there is a ButtonField with a "Select" button but I would like the Select command to take place as or after the grid is populated.
Thanks in advance.
<asp:GridView ID="ClientDetailsGridView" runat="Server" AutoGenerateColumns="False" DataKeyNames="AccountNumber" DataSourceID="ZeNetAccounts" CellPadding="2" ForeColor="#333333" GridLines="None" Width="749px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Select" HeaderText="More Information" Text="View" />
<asp:BoundField DataField="AccountNumber" HeaderText="Account #" SortExpression="AccountNumber" />
<asp:BoundField DataField="AccountName" HeaderText="Client Name" SortExpression="AccountName" />
<asp:BoundField DataField="Address1" HeaderText="Address" SortExpression="Address1" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
<asp:BoundField DataField="PostalCode" HeaderText="Zip" SortExpression="PostalCode" />
<asp:BoundField DataField="PrimaryPhoneNumberFormatted" HeaderText="Phone" SortExpression="PrimaryPhoneNumberFormatted" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Transparent" Font-Bold="True" ForeColor="White" Height="25px" CssClass="GridHeaderBasic" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SelectedRowStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SortedAscendingCellStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SortedAscendingHeaderStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SortedDescendingCellStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SortedDescendingHeaderStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
</asp:GridView>
<br />
<br />
<asp:GridView ID="CustomFieldsGridView" runat="server" AutoGenerateColumns="False" SelectedIndex="0" DataKeyNames="DocumentNumber,DocumentType,CustomFieldDefinitionKeyID" DataSourceID="ZeNetCustomFields">
<Columns>
<asp:BoundField DataField="DocumentNumber" HeaderText="DocumentNumber" ReadOnly="True" SortExpression="DocumentNumber" />
<asp:BoundField DataField="DocumentType" HeaderText="DocumentType" ReadOnly="True" SortExpression="DocumentType" />
<asp:BoundField DataField="CustomFieldDefinitionKeyID" HeaderText="CustomFieldDefinitionKeyID" ReadOnly="True" SortExpression="CustomFieldDefinitionKeyID" />
<asp:BoundField DataField="CustomFieldValue" HeaderText="CustomFieldValue" SortExpression="CustomFieldValue" />
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="ZeNetAccounts" runat="Server" ConnectionString="<%$ ConnectionStrings:Ze-Net_Technologies %>"
SelectCommand="SELECT * FROM [tblAccounts] WHERE ([AccountName] = #AccountName)">
<SelectParameters>
<asp:ControlParameter ControlID="ClientSearch" Name="AccountName" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="ZeNetCustomFields" runat="Server" ConnectionString="<%$ ConnectionStrings:Ze-Net_Technologies %>"
SelectCommand="SELECT * FROM [tblCustomFieldValues] WHERE ([DocumentNumber] = #DocumentNumber)">
<SelectParameters>
<asp:ControlParameter ControlID="ClientDetailsGridView" Name="DocumentNumber" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
I have this GridView in my page:
<asp:GridView ID="GridView2" runat="server" DataSourceID="SqlDataSource2"
AllowPaging="True" PageSize="20" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
And this is the SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT country AS Country,count(country) As Count FROM client GROUP by country ORDER BY count(country) DESC;" ></asp:SqlDataSource>
And i want that the name will be hyper link to somethis like this:
/Details.aspx?country=countryname
How i can implement it?
Use a <TemplateField> with a HyperLink control in it, like this:
<asp:GridView ...>
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:HyperLink runat="server" ID="HyperLink1"
NavigateUrl='<%# "Details.aspx?country=" + Eval("Country")%>'
Text='Details'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HyperLink runat="server" ID="HyperLink1"
NavigateUrl='<%# "/Details.aspx?country=" + Eval("Country")%>'
Text='Details'>
</asp:HyperLink>
I've got a gridview that draws from multiple tables, I'd like to make it searchable in three fields, one from each table. I plan on using a dropdown to select which field to search for, and a textbox of course for search input, but what then? Here's my gridview code in C# ASP.NET:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display." AllowPaging="True"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical" HorizontalAlign="Center"
DataKeyNames="SubId" AllowSorting="True">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="SubId" HeaderText="Submission ID"
SortExpression="SubId" >
<ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="CustName" HeaderText="Customer"
SortExpression="CustName" />
<asp:BoundField DataField="CustCity" HeaderText="Customer City"
SortExpression="CustCity" />
<asp:BoundField DataField="CustState" HeaderText="Customer State"
SortExpression="CustState" />
<asp:BoundField DataField="BroName" HeaderText="Broker "
SortExpression="BroName" />
<asp:BoundField DataField="BroState" HeaderText="Broker State"
SortExpression="BroState" />
<asp:BoundField DataField="EntityType" HeaderText="EntityType"
SortExpression="EntityType" />
<asp:BoundField DataField="Coverage" DataFormatString="{0:c}"
HeaderText="Coverage" SortExpression="Coverage" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
<asp:HyperLinkField DataNavigateUrlFields="SubId"
DataNavigateUrlFormatString="View.aspx?SubId={0}" Text="View" />
<asp:HyperLinkField DataNavigateUrlFields="SubId"
DataNavigateUrlFormatString="ViewEdit.aspx?SubId={0}" Text="Edit" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
ProviderName="<%$ ConnectionStrings:ProductInstanceString.ProviderName %>"
SelectCommand="SELECT Customer.SubId, Customer.CustName, Customer.CustCity, Customer.CustState, Broker.BroName, Broker.BroState, Broker.EntityType, Submission.Coverage, Submission.Status FROM Submission INNER JOIN Broker ON Broker.SubId = Submission.SubmissionId INNER JOIN Customer ON Customer.SubId = Submission.SubmissionId">
</asp:SqlDataSource>
Use dropdown on the page and on the codebehind use DataView object to filter rows of grid, see below syntax:
DataView dvData = new DataView(dtData);
dvData.RowFilter = "[ColumnName]=[Condition, this will be your dropdown value]";
gv.DataSource = dvData;
gv.DataBind();