I have a GridView where one of the columns/fields has checkboxes; the checkboxes are checked or unchecked, based on a sub-query from the code behind, while the container GridView is bound to a SqlDataSource. What I want to be able to do is that once the GridView is databound then make it sort by the state of checkboxes: All rows with checkboxes checked appear on the top of grid. Here is part of my GridView:
<ItemTemplate>
<asp:CheckBox ID="ProductSelector" runat="server" Checked='<%# ShowCheckMarks(DataBinder.Eval(Container.DataItem,"prodid").ToString()) %>' />
</ItemTemplate>
I am thinking that I can call the GridView's Ondatabound event and someone make it sort from there?
Thanks.
If that is the only row that you need to sort by why not just add an order by to the sql query you're using to pull the data?
You can save yourself a lot of work if you
store the state of checkbox in database
. That way you can easily set a sort expression on the template field.
Edit
You can do this on clientside via jQuery tablesorter but it might be a
bit too much of work Check this.
Again I think you can somehow manipulate the query to achieve this.
Try this
SELECT dbo.Products.*, dbo.products_recommended.* FROM dbo.Products INNER JOIN dbo.products_recommended ON (dbo.Products.prodid = dbo.products_recommended.prodid) WHERE dbo.Products.prodid IN (dbo.products_recommended.prodid) AND (dbo.Products.prodid = " + itemid + " order by dbo.Products.prodid desc )"
Please note : Never use a
select a.* from YourTable a
This will select all columns from your table & may bring a serious problem later on. Only query the columns you want like
select a.column1,a.column2 from YourTable a
SELECT dbo.Products.prodid,
dbo.Products.itemtitle,
dbo.Products.itemnumber,
dbo.Products.image_1,
CAST(ISNULL(dbo.products_recommended.recommendedid, 0) as BIT) as recommendedid
FROM dbo.Products LEFT OUTER JOIN dbo.products_recommended
ON (dbo.Products.prodid = dbo.products_recommended.prodid)
WHERE dbo.Products.prodid IN (dbo.products_recommended.prodid)
AND (dbo.Products.prodid = #itemid)
The left outer join will make sure it pulls all items from the Products table while only pulling matching items from the products_recommended table. It will also convert the recommendedid to a BIT value which should work with the checkboxes
Like the good helpers here suggested, I needed to have main sql query to populate the GridView to sort the records, instead of a different query in the code behind. So now the Declarative SqlDataSource syntax is the following. This solves the problem.
I learned a few hew things from the feedback here. Thank you all!
SELECT DISTINCT Products.prodid,
Products.itemtitle,
Products.itemnumber,
Products.image_1,
CASE WHEN YESNO IS NULL THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT) END AS CheckUncheck
FROM Products
LEFT OUTER JOIN (SELECT prodid_recommended as YESNO FROM products_recommended
WHERE #itemid IN (prodid) ) A
ON Products.prodid = A.YESNO
ORDER BY CheckUncheck DESC
Related
Ok so I have been searching for three days and so far I can not find an answer that explains what I need to do.
Here is what I need. I have an asp.net content page. On that page there are some drop down list boxes. I have a query that fetches data from a SQL DB and puts in a Data table and that is working fine.
What I am trying to do is set the text of the ddl to a string that has been returned from the query.
Here is the HTML side:
<asp:DropDownList ID="ddlProbType" CssClass="ddlEdit" runat="server" Width="150px" DataSourceID="SdsProbType" DataTextField="probtype" DataValueField="probtype" />
Here is the VB code behind:
If dt.Rows.Count > 1 Then
stupidString = dt.Rows(RowCount)("probtype")
ddlProbType.SelectedValue = stupidString
Else
stupidString = dt.Rows(RowCount)("probtype")
ddlProbType.SelectedValue = stupidString
End If
I have added the variable stupidString and the set a break point to check the value being returned and it is correct. When the next line executes the ddl does NOT get the text assigned. I can mouse over the variable stupidString and see that the value it was assigned from the query is correct.
When I have tried setting the ddl directly from the data table, I have tried .ToSting() and .ToString.Trim() to the end of the dt.Rows line but it just will not assign!!
I know this probably something really stupid that I am not doing or overlooking but can anyone help? I do not want to assign a whole sql table to the ddl I just want to set the current list to the value that is returned by the sql query.
You need to populate the list first, e.g. by using
ddlProbType.Items.Add(New ListItem("Key","Value"))
or by using Data Binding.
After the list is populated, then you can tell the control which item should be selected.
I have a drop down list inside of a DataList EditItemTemplate, fueled by a SQLDataSource (below). The parameter #panelid is being set in codebehind on the SQL databinding event, and that seems to be working fine. Contents are accurate and what I expected.
Then I tried setting the selected value using Bind("scopeid"), which should be fine. scopeid and equipmentid are related, in fact scopeid is populated by equipmentid in the footer of this same datalist, so their values should be matching (and they are). This should push the scopeid of the chosen record to the selected item in the ddl. However, I get the 'ddlEquipment' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value error when switching to my EditItemTemplate. Any ideas?
<asp:DropDownList class="smallInputddl" ID="ddlEquipment" runat="server" DataSourceID="sqlEditEquipment" SelectedValue='<%# Bind("scopeid")%>' DataTextField="modelnumber" DataValueField="equipmentid" AppendDataBoundItems="true">
</asp:DropDownList>
<asp:SqlDataSource ID="sqlEditEquipment" runat="server" OnDataBinding="sqlEditEquipment_DataBinding" ConnectionString="<%$ ConnectionStrings:ProductionDatabaseConnectionString1 %>"
SelectCommand="select * from tblsls_equipmentscope where proposalnumber in (select proposalnumber from tblsls_cntrlpanel where id = #panelid)">
<SelectParameters>
<asp:Parameter Name="panelid" />
</SelectParameters>
</asp:SqlDataSource>
To demonstrate, here's a Sql query to show the relationship between scopeid and equipmentid. tblsls_cntrlvfd also has the id from tblsls_cntrlpanel.:
select e.equipmentid, c.scopeid from tblsls_equipmentscope e
left join tblsls_cntrlvfd c on e.equipmentid = c.scopeid
where proposalnumber in (select proposalnumber from tblsls_cntrlpanel where id = 20)
Results:
equipmentid scopeid
----------- --------
9513 9513
9541 9541
9543 NULL
(3 row(s) affected)
The gist is that the equipment exists off in it's own little world, and controls in it's. Both get tied to a proposal. There's also a VFD that is associated to both a control panel and a specific piece of equipment.
You'll need to include a 'blank' option in your DDL for it to match the null value to. It's trying to find a match in the list to the cell's original value(null) but since that isn't an available option it's throwing the exception. Just set index 0 of your DDL to be blank.
Figured it out. I'm in the habit of keeping SQL data sources that just fuel a drop down list right with the list they populate. Usually this is desired. In this situation, keeping it with the list resulted in me having to use a work around to set it's parameter's value. It could not see the label it needed to from where it was, so I was setting the default value in the data sources Databinding event. This works fine on it's own, and the list populates as expected. As soon as you try to set the SelectedValue however, when the datasource is having it's parameters setup in databinding, what happens is a slight loading out of order issue. These eventually wind up running at the same time, so the datasource hasn't run and populated it's dataset yet, SelectedValue sees a set with 0 entries in it.
Moving the SQLDataSource out so that it could see the control I originally wanted it to solved the issue. It no longer needed to have it's parameter set through code, it's list is essentially static.
Currently, I'm populating a DropDown with the following SQL:
SELECT REPORT_DATE FROM MY_TABLE
Although the values stored in the table are in the following format, dd-mon-yyyy (my preferred format), the text that fills my DropDown differs in that it also displays time, like dd-mon-yyyy hh:mm:ss AM.
What is the best way to resolve this? I know of TRUNC and TO_DATE functions in Oracle SQL. But I think I could also loop through the DropDown after its populated and truncate the string there. I have tried the following code, but it returns a runtime error:
For Each i As ListItem In DropDown1.Items
'Strip time here
i.Text.ToString("dd-MMM-yyyy")
Next
Essentially, I just want to loop my DropDown and change ONLY the text to match `dd-MMM-yyyy'. How can I accomplish this? Should I use SQL or VB.NET? Is there a best practice?
Thank you!
you can do this sql side with
SELECT to_char(REPORT_DATE, 'dd-mon-yyyy') report_date FROM MY_TABLE order by report_date;
p.s.
"values stored in the table are in the following format, dd-mon-yyyy". dates are not stored like that, they are stored as an 7 byte numeric internally, how you see them on select is entirely dependant on your NLS_DATE_FORMAT setting.
If the data is of type DateTime, you can simply specify the DataTextFormatString property on the dropdown as so:
<asp:dropdownlist DataTextFormatString ="{0:MM-dd-yyyy}" ... />
It should be to the UI layer to display the date in the format you want. You should try to avoid putting the logic to transform the date to the format you want on the SQL statement, if possible.
Use this string to get data from Oracle
SELECT TO_CHAR(REPORT_DATE,'DD-MON-YYYY') FROM MY_TABLE
Try
i.Text = Convert.ToDateTime(i.Text).ToString("dd-MMM-yyyy")
You cannot use .ToString("dd-MMM-yyyy") for date formatting on string. You need to convert it to DateTime
You can also try <asp:DropDownList DataTextFormatString="{0:dd-MMM-yyyy}" />
It's better to format the date in the DropDownList instead of letting sql doing it.
Just add DataTextFormatString="{0:dd/MM/yyyy}" to your DropDownList tag :
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="adddate"
DataTextFormatString="{0:dd/MM/yyyy}" DataValueField="adddate">
</asp:DropDownList>
My Asp.net page has multiple textboxes and DropDownLists that populate from a selection in the Gridview. My problem is multiple, I have a keyfield in the gridview that is a uniqueidentifier used to insert, update, and delete records from my database. I do not want theis column to be seen, I have used a css
.hideCol
{
display: none;
}
But it isn't working, I also tried setting Visible = "False" and it will not populate the textboxes. I need to find out how to hide that column so the user doesn't see it.
My new problem is my DropDownLists consist of a sql query from one table and concatenating multiple columns.
SELECT [Column1] + ' | ' + [Column2]
FROM [Table1]
ORDER BY [Column2]
So my result looks like this is the DropDown:
123456 | Name
When I try and populate my DropDownLists and Textboxes from the selected row of my gridview I get an error on the DropDownList because it is not recognized as a value.
123456 is what is in the database I need it to populate based on those numbers but display in the format above? Does this make sense? any suggestions?
UPDATE:
AllowPaging="True" CellPadding="4" CssClass="td3" ForeColor="#333333">
I have a dropdownlist populated with a list of locations. The datasource is an ObjectDataSource named objectDataSourcePlaces. The DataValueField is PlaceID. The DataTextField, the value I want the user to see, resides in another table.
I'm guessing I need to perform some sort of JOIN to get the display string from the second table. The question, how do I get the display string from the second table and use that in my dropdownlist?
Yes, you would join the tables on the DataValueField and select the text field.
For example:
Select T1.PlaceId, T2.Text as DTField
from Table1 T1
left join Table2 T2 on T1.PlaceID= T2.PlaceId
Where some condition
Additional Info
I see you're having trouble in ASP.net side. all you'd have to do is set the DataTextField to the text field you selected via the joined tables. In my example above you'd do something like this:
DataTextField = "DTField"
You can do this in code on Load or whenever it is needed or in the Markup as an attribute of the drop down. Hope this helps.