dropdownlist populate gridview - c#

I am using the wizard in Visual Studio to define the a data source that populates a dropdownlist. This is working fine. Then I have a second datasource that should filter data from another table using the selected value from the dropdownlist and display it in a gridview. When I make my selection in the dropdownlist I thought the gridview should changed automatically to show the new filtered data. But it is not. Seems like I am missing an auto post back or something like that. Below is the code I am using.
<form id="appointmentform" runat="server">
<div class="wdn-band" id="findpersonband" runat="server" visible="true">
<div class="wdn-inner-wrapper wdn-inner-padding-sm">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SNR_InformationConnectionString %>" SelectCommand="SELECT [personnelid], [lastname] FROM [tblmasterpersonnel] ORDER BY [lastname], [preferredfirstname]"></asp:SqlDataSource>
<asp:DropDownList ID="MasterPersonnelDDL" runat="server" DataSourceID="SqlDataSource1" DataTextField="lastname" DataValueField="personnelid"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:SNR_InformationConnectionString %>" SelectCommand="SELECT [personnelid], [startdate], [endingdate], [employeetypeid], [dateentered] FROM [tblappointmentdate] WHERE ([personnelid] = #personnelid)">
<SelectParameters>
<asp:ControlParameter ControlID="MasterPersonnelDDL" Name="personnelid" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="startdate" HeaderText="startdate" SortExpression="startdate" />
<asp:BoundField DataField="endingdate" HeaderText="endingdate" SortExpression="endingdate" />
<asp:BoundField DataField="employeetypeid" HeaderText="employeetypeid" SortExpression="employeetypeid" />
<asp:BoundField DataField="dateentered" HeaderText="dateentered" SortExpression="dateentered" />
</Columns>
</asp:GridView>
</div>
</div>
</form>
Thanks for the help

may be you could use
<asp:DropDownList ID="MasterPersonnelDDL" runat="server" DataSourceID="SqlDataSource1" DataTextField="lastname" DataValueField="personnelid" AutoPostBack="True"></asp:DropDownList>

Related

DropDownList DataSourceID in ListView DTO

Why can't I use SelectedValue on my DropDownList !?
I tried SelectedValue="<% BindItem.current_contact.status_id %>" but the error says it is not a valid parameter. And Bind("status_id") does not work either: "The server tag is not correct." on my DropDownList.
<asp:SqlDataSource runat="server" ID="SdsStatus"
SelectCommand="SELECT SP.status_id, SP.sp_label, SP.sp_order FROM tr_status SP WHERE SP.groupe_id = #groupe_id ORDER BY SP.sp_order ; " ConnectionString="<%$ ConnectionStrings:DBLOGIPRO %>">
<SelectParameters>
<ctrl:ClassPropertyParameter Name="groupe_id" ClassName="User" PropertyPath="CurrentNego.groupe_id" ConvertEmptyStringToNull="true" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:ListView runat="server" ID="LvContact"
RenderOuterTable="false"
SelectMethod="GetAccountWithContacts" ItemType="AccountWithContactsDTO"
UpdateMethod="UpdateAccountWithContacts">
<EditItemTemplate>
...
<asp:DropDownList runat="server" ID="DdlStatus" EnableViewState="true"
DataSourceID="SdsStatus"
DataTextField="sp_label" DataValueField="status_id"
DataTextFormatString="{0}" AppendDataBoundItems="true"
OnSelectedIndexChanged="DdlStatus_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem Value="" Text="" Selected="True"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:ListView>
You can try set the SelectValue in ListView's ItemDataBound event. There you can handle controls' attribute values.
I am not sure if you can handle this kind of attribute with Bind() method directly in aspx.

Asp.net update DropDownList selected item

I'm stuck into a problem using a DropDownList item into a form.
The main form is feed by an SQLDataSource ... all form widget are bound to a specific record field etc.
I changed one of the form widget (a TextBox) with a DropDownList to allow the user to only select between a number of values... I feed this control trough another SQLDataSource. While the form is in Insert mode there are no problems but when I switch into Edit mode I'm not able to set the DropDownList selected item with the value coming from the main record. Ho can I fix this?
Here's a sample of my .aspx code:
<asp:FormView ID="_frmData" runat="server" DataKeyNames="id" DataSourceID="_sdsTable1" DefaultMode="Insert">
<InsertItemTemplate>
<asp:TextBox ID="_txtField0" runat="server" Text='<%#Bind("field0")%>'/>
<asp:DropDownList ID="_ddlField1" runat="server" DataSourceID="_sdsTable2" DataTextField="field_text" DataValueField="field_value" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="None" Selected="True" />
</asp:DropDownList>
</InsertItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="_txtField0" runat="server" Text='<%#Bind("field0")%>'/>
<asp:DropDownList ID="_ddlField1" CssClass="input_medium" runat="server" DataSourceID="_sdsTable2" DataTextField="field_text" DataValueField="field_value" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="None" Selected="True" />
</asp:DropDownList>
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="_sdsTable1" runat="server" ConnectionString="<%$ ConnectionStrings:_db %>"
ProviderName="<%$ ConnectionStrings:_db.ProviderName %>"
SelectCommand=" SELECT
field0, field1
FROM
table1
WHERE
id=#id;"
UpdateCommand=" UPDATE
table1
SET
`field0` = #field0
`field1` = #field1
WHERE
id = #id;""
InsertCommandType="StoredProcedure"
InsertCommand="create_table1_entry">
<InsertParameters>
<asp:ControlParameter Name="field1" ControlID="_frmData$_ddlField1" Type="String" PropertyName="SelectedItem.Text" />
</InsertParameters>
<UpdateParameters>
<asp:ControlParameter Name="field1" ControlID="_frmData$_ddlField1" Type="String" PropertyName="SelectedItem.Text" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="_sdsTable2" runat="server" ConnectionString="<%$ ConnectionStrings:_db %>"
ProviderName="<%$ ConnectionStrings:_db.ProviderName %>"
SelectCommand=" SELECT
field_value, field_text
FROM
table2;" />
Try This
myDropDown.Items.FindByValue("Your Main Record Value").Selected=true;
OR
myDropDown.Items.FindByText("Your Main Record Text").Selected=true;
I CAN JUST use SelectedValue='<%#Bind("field1")%>' property in DDL to achieve the result, just as I was guessing. The only issue here is the why Intellisense is not suggesting SelectedValue as a propery for DropDownList ... thnx everyone.

Why is my gridview empty?

I set up an Sqldatasource and a Gridview:
<asp:SqlDataSource ID="DetailsSQLDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-WebApplication6-20131007103938ConnectionString1 %>"
SelectCommand="SELECT [ProjectID], [ProjectName], [CreatorID], [Deadline] FROM [tProject] WHERE 1=1"
>
<SelectParameters>
<asp:Parameter Type="String" Name="ProjectID"></asp:Parameter>
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="DetailsSQLDataSource" AutoGenerateColumns="False" DataKeyNames="ProjectID" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="ProjectID" HeaderText="ProjectID" ReadOnly="True" SortExpression="ProjectID"></asp:BoundField>
<asp:BoundField DataField="ProjectName" HeaderText="ProjectName" SortExpression="ProjectName"></asp:BoundField>
<asp:BoundField DataField="CreatorID" HeaderText="CreatorID" SortExpression="CreatorID"></asp:BoundField>
<asp:BoundField DataField="Deadline" HeaderText="Deadline" SortExpression="Deadline"></asp:BoundField>
</Columns>
</asp:GridView>
I have tested the selectcommand, and it returns a table with 4 rows. Yet there is no data to show in the gridview. The codebehind is almost empty and surely doesn't affect these controls. Can it be something with the connection? How can I test that?
Call .DataBind() to actually force the data to be bound to the grid, like this:
GridView1.DataBind();

clicking on link button in gridview does not show the details in detailsview

I have a grid view and a details view. In that grid view I have a link button column in the first column. When I click on the link button column it has to show the corresponding row values in the details view. I am using separate data sources for details view and grid view.
This is the grid view code:
<asp:GridView ID="GridView2" runat="server"
DataSourceID="SqlDataSourcegridview" Height="533px" Width="316px"
style="text-align: left" EnableCallBack="false"
AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField ShowHeader="false">
<HeaderTemplate>
<asp:Label ID="lbl1" runat="server" Text="DCN"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkname" runat="server" SortExpression="DCN"
Text='<%#Eval("DCN") %>' CausesValidation="false"
CommandName='<%#Eval("DCN")%>' OnClick = "Link_buttonClick">
linkbutton
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="priority" HeaderText="priority"
SortExpression="priority" />
</Columns>
</asp:GridView>
Datasource for gridview:
<asp:SqlDataSource ID="SqlDataSourcegridview" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectDBString %>"
SelectCommand="SELECT * FROM [MASTERETMTABLE] WHERE ([DCN] = #DCN)">
<SelectParameters>
<asp:Parameter DefaultValue="BAT" Name="DCN" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Details view:
<asp:Panel ID="paneldetails" runat="server" BackColor="#CCCCCC"
Visible="False" >
<asp:DetailsView ID="detailsvw" runat="server" AutoGenerateRows="False"
DataSourceID="SqlDataSourcedetailsview" Width="424px">
<Fields>
<asp:BoundField DataField="DCN" HeaderText="DCN" SortExpression="DCN" />
<asp:BoundField DataField="PRIORITY" HeaderText="PRIORITY"
SortExpression="PRIORITY" />
</Fields
</asp:DetailsView>
</asp:Panel>
and this is my datasource for details view
<asp:SqlDataSource ID="SqlDataSourcedetailsview" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectDBString %>"
SelectCommand="SELECT * FROM [MASTERETMTABLE] WHERE ([DCN] = #DCN)">
<SelectParameters>
<asp:ControlParameter Name="DCN" ControlID="GridView2"/>
</SelectParameters>
</asp:SqlDataSource>
This is the code behind file for the gridview page.
protected void Link_buttonClick(object sender, EventArgs e)
{
paneldetails.Visible = true;
detailsvw.Visible = true;
// do I need to add any thing at here
}
I am not able to get the data into details view here. When I click on any cell in the first column it shows the details view in panel.
How do I get the details view to display the data from the clicked row?
The easy way to implement Master-Details relationship is to add the DataKeyNames="DCN" (pertinent to your case) to the first GridView and refer to it as a Parameter in Details form: see the sample here: http://www.vkinfotek.com/detailsview/gridview-detailsview-master-detail-example.html

Using filters with gridview and stored procedures in ASP.NET and C#

I have a gridview that's populated by a stored procedure. On this dataset, the user should be able to filter it by selecting the LabID and/or SiteName from 2 dropdownlists. What is the best way to accomplish this?
<asp:DropDownList ID="ddlLabIDs" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource2" DataTextField="LabID"
DataValueField="LabID" AppendDataBoundItems="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="exec usp_LabIDs_Select;"></asp:SqlDataSource>
<asp:DropDownList ID="ddlSiteNames" runat="server"
DataSourceID="SqlDataSource3" DataTextField="SiteName"
DataValueField="SiteName" AppendDataBoundItems="True"
AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="exec usp_SiteNames_Select;"></asp:SqlDataSource>
<asp:GridView ID="AllDataFlat" runat="server" AllowPaging="True" PageSize="20"
AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="LabID" HeaderText="LabID" SortExpression="LabID" />
<asp:BoundField DataField="SiteName" HeaderText="SiteName"
SortExpression="SiteName" />
<asp:BoundField DataField="StartDateTime" HeaderText="StartDateTime"
SortExpression="StartDateTime" />
<asp:BoundField DataField="FilterNumber" HeaderText="FilterNumber"
SortExpression="FilterNumber" />
<asp:BoundField DataField="AmtWaterFiltered" HeaderText="AmtWaterFiltered"
ReadOnly="True" SortExpression="AmtWaterFiltered" />
<asp:BoundField DataField="WaterTemp" HeaderText="WaterTemp" ReadOnly="True"
SortExpression="WaterTemp" />
<asp:BoundField DataField="pH" HeaderText="pH" SortExpression="pH" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="exec usp_AllDataFlat_Select;">
</asp:SqlDataSource>
Thank you.
you can try like this.....
NOTE : this is just example on how to filter the grid view with the selected values on two drop down lists...
<%# page autoeventwireup="true" codefile="FilterWithMultipleDropDownLists.aspx.cs"
inherits="GridView_FilterWithMultipleDropDownLists" language="C#" masterpagefile="~/MasterPages/Default.master"
title="GridView: Filter With Multiple DropDownLists" %>
<asp:content id="Content1" runat="Server" contentplaceholderid="ContentPlaceHolder1">
<div>
Select Supplier:
<asp:dropdownlist id="ddlSuppliers" runat="server" appenddatabounditems="True" autopostback="True"
datasourceid="sdsSuppliers" datatextfield="CompanyName" datavaluefield="SupplierID">
<asp:listitem text="All Suppliers" value="-1">
</asp:listitem>
</asp:dropdownlist>
<asp:sqldatasource id="sdsSuppliers" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
selectcommand="SELECT [SupplierID], [CompanyName] FROM [Suppliers] ORDER BY [CompanyName]">
</asp:sqldatasource>
</div>
<div style="margin-top: 12px;">
Select Category:
<asp:dropdownlist id="ddlCategories" runat="server" appenddatabounditems="True" autopostback="True"
datasourceid="sdsCategories" datatextfield="CategoryName" datavaluefield="CategoryID">
<asp:listitem text="All Categories" value="-1">
</asp:listitem>
</asp:dropdownlist>
<asp:sqldatasource id="sdsCategories" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
selectcommand="SELECT [CategoryID], [CategoryName] FROM [Categories] ORDER By [CategoryName]">
</asp:sqldatasource>
</div>
<asp:gridview id="gvProducts" runat="server" autogeneratecolumns="False" datakeynames="ProductID"
datasourceid="sdsProducts" style="margin-top: 24px;">
<columns>
<asp:boundfield datafield="ProductID" headertext="ProductID" insertvisible="False"
readonly="True" sortexpression="ProductID" />
<asp:boundfield datafield="ProductName" headertext="ProductName" sortexpression="ProductName" />
<asp:boundfield datafield="CompanyName" headertext="Supplier" sortexpression="CompanyName" />
<asp:boundfield datafield="CategoryName" headertext="Category" sortexpression="CategoryName" />
</columns>
</asp:gridview>
<asp:sqldatasource id="sdsProducts" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
selectcommand="SELECT [P].[ProductID], [P].[ProductName], [P].[SupplierID], [P].[CategoryID], [S].[CompanyName], [C].[CategoryName] FROM [Products] AS [P] INNER JOIN [Suppliers] AS [S] ON [S].[SupplierID] = [P].[SupplierID] INNER JOIN [Categories] AS [C] ON [C].[CategoryID] = [P].[CategoryID] WHERE ([P].[SupplierID] = CASE WHEN #SupplierID = -1 THEN [P].[SupplierID] ELSE #SupplierID END AND [P].[CategoryID] = CASE WHEN #CategoryID = -1 THEN [P].[CategoryID] ELSE #CategoryID END) ORDER BY [P].[ProductName]">
<selectparameters>
<asp:controlparameter controlid="ddlSuppliers" name="SupplierID" propertyname="SelectedValue" type="Int32" />
<asp:controlparameter controlid="ddlCategories" name="CategoryID" propertyname="SelectedValue" type="Int32" />
</selectparameters>
</asp:sqldatasource>
</asp:content>
I hope it will helps you..
The most efficient way to do this would be to pass the values selected in the drop down controls to the stored procedure usp_AllDataFlat_Select. In this stored procedure you should be able to select only the rows that satisfy the filter values.
If you do not have access to the stored procedures (can't modify them) than in my oppinion the best way to achieve this is to wrap your call to the stored procedure in an object that retrieves all the rows from the data base and filters them in memory (not very efficient). Then you can use an OnjectDataSource instead of the SqlDataSource to populate the gridview. This ObjectDataSoruce could call the methods of the object that wraps your stored procedures.
You can find an example here. The example uses queries instead of stored procedures but replacing the queries with calls to stored procedures should be trivial.
Best of luck,

Categories