How to make gridview searchable? - c#

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

Related

Exception Details: System.NotSupportedException: Deleting is not supported by data source 'SqlDataSourceRegister' unless DeleteCommand is specified

Im getting this error when im trying to enable an delete button on my asp.net website, any solutions ??
error : Exception Details: System.NotSupportedException: Deleting is not supported by data source 'SqlDataSourceRegister' unless DeleteCommand is specified.
<asp:SqlDataSource ID="SqlDataSourceRegister" runat="server" ConnectionString="<%$ ConnectionStrings:RegisterConnectionString %>" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSourceRegister" ForeColor="#333333" GridLines="None" Height="195px" Width="254px" DataKeyNames="Id" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" ReadOnly="True" InsertVisible="False" />
<asp:BoundField DataField="Användarnamn" HeaderText="Användarnamn" SortExpression="Användarnamn" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="Lösenord" HeaderText="Lösenord" SortExpression="Lösenord" />
<asp:BoundField DataField="Land" HeaderText="Land" SortExpression="Land" />
<asp:BoundField DataField="Fullnamn" HeaderText="Fullnamn" SortExpression="Fullnamn" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
</form>
</body>
</html>

Is there a way to automatically execute the 'Select' command on a GridView, when the Grid loads?

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>

Identify the Access Key of a particular record in Gridview ,(For a button / TemplateField)

I am trying to have a gridview with dynamic data imported using a SqlDataSource , plus 2 TemplateFields containing 2 buttons as Accept and Decline.What I need to do is , update a specific column of the table where the records are coming from.(Change the value from Null to 1 or 0, in the column called "accepted")
Q.)I need a way to identify the row which the button will be clicked ?
(need to identify the value of access key for the particular row)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="request_no" DataSourceID="SqlDataSource1" Font-Names="Tahoma" Font-Size="Small" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="request_no" HeaderText="request_no" ReadOnly="True" SortExpression="request_no" />
<asp:BoundField DataField="employee_name" HeaderText="employee_name" SortExpression="employee_name" />
<asp:BoundField DataField="requested_date" HeaderText="requested_date" SortExpression="requested_date" />
<asp:BoundField DataField="requested_time" HeaderText="requested_time" SortExpression="requested_time" />
<asp:BoundField DataField="requested_from" HeaderText="requested_from" SortExpression="requested_from" />
<asp:BoundField DataField="requested_to" HeaderText="requested_to" SortExpression="requested_to" />
<asp:BoundField DataField="service_type" HeaderText="service_type" SortExpression="service_type" />
<asp:BoundField DataField="reason_data" HeaderText="reason_data" SortExpression="reason_data" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AcceptRequest" runat="server" OnClick="AcceptRequest_Click" Text="Accept" CommandName="AcceptRequestcmd" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="DeclineRequest" runat="server" OnClick="DeclineRequest_Click" Text="Decline" CommandName="DeclineRequestcmd" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<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>
You are very close, just lack CommandArgument:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AcceptRequest" runat="server"
OnClick="AcceptRequest_Click" Text="Accept"
CommandName="AcceptRequestcmd"
CommandArgument="<%# Eval("request_no") %> />
</ItemTemplate>
</asp:TemplateField>
(assuming that request_no is the row identifier)
Then just
protected void GridView1_RowCommand(...)
{
switch ( e.CommandName )
{
case "AcceptRequestcmd" :
string rowid = e.CommandArgument;
}
}

GridView columns header text

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"

Enable multiple select in GridView with custom data source

I have this GridView that uses a custom data source:
<asp:ScriptManager ID="DateManager" runat="server" />
<asp:UpdatePanel ID="setDate" runat="server"
UpdateMode="Conditional"
ChildrenAsTriggers="true">
<ContentTemplate>
<asp:GridView ID="res" runat="server"
AutoGenerateColumns="False"
CellPadding="4"
ForeColor="#333333"
GridLines="None"
Width="600px"
AllowPaging="True">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Ty" HeaderText="Type" />
<asp:BoundField DataField="time" HeaderText="Time" />
<asp:BoundField DataField="duration" HeaderText="Duration" />
<asp:BoundField DataField="confirmed" HeaderText="Status" />
<asp:CommandField SelectText="SelectIt" ShowSelectButton="True" />
</Columns>
<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>
</ContentTemplate>
</asp:UpdatePanel>
How can I enable multiple select on it?
You can add CheckBox near evey row like code below and select multiple lines.
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="RowLevelCheckBox" />
</ItemTemplate>
</asp:TemplateField>
For detailed information check these articles : Article1 Article2

Categories

Resources