populating a drop down list from 2 columns from database - asp.net

I have a table user in my database containing 2 columns first name and last name.
I want to populate a drop down list so the user can choose from the drop down list a person according to his first and last name.
So how can i enter first and last name into one drop down list?
This my drop down list so far.
<telerik:GridDropDownColumn DataField="DepIDVoit"
HeaderText="Voiture" UniqueName="IdVoiture4"
ListTextField="VMarque" DropDownControlType="DropDownList"
ListValueField="VVID" FilterControlAltText="Filter VMarque column"
ShowFilterIcon="false" CurrentFilterFunction="Contains"
AutoPostBackOnFilter="true"
DataSourceID="SqlDataSource3">
<ColumnValidationSettings
EnableRequiredFieldValidation="true" >
<RequiredFieldValidator ForeColor="Red"
ErrorMessage="* Mandatory Field">
</RequiredFieldValidator>
</ColumnValidationSettings>
</telerik:GridDropDownColumn>
I want to put in the ListTextField : VMarque and VModel separeted with a space

Related

How can I prevent the selection of all radio buttons in radio button lists that have the same ID value?

I have multi radio button lists. every one of them is showing on drop down list index changed. My problem is when I select a radio button from a specific radio button list, all of the other radio buttons lists select their own radio button that have the same ID value with the one I selected.
so when I select one, all others select their own.
I am using Vb.NET web forms and I set the radio button lists auto post back true and they are being created dynamically.
And when I make a unique ID it does not allow me to select so when I select it make the radio button not selected.
This is perhaps one of those cases in which the stated goal is more valuable.
You don't mention or note where you data is comming from.
Lets say I have a list of hotels. And I want a radio button list of the "rating" for the hotel.
Well, in the forms desinger - drop in a gridview. Generate the columns. (then delete that data source from the markup. and in properties sheet remove the dataSource1 setting.
Ok, so far? That's about 2 minutes of time.
Now, drop in a radio buttion list. (somtimes I drop it on the form outside of the grid. Select the radio list and then add the choices (0-5 rating).
Now move that radio button list into the grid view. You have to wrap it around a item template.
So note how I used the builders here most of the time. (EVEN for the choices in the radiobutton list I used the builder from the designer).
Ok, so now our markup looks like this:
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" SortExpression="HotelName" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:TemplateField HeaderText="Rating For Hotel">
<ItemTemplate>
<asp:RadioButtonList ID="RadioButtonList2" runat="server" RepeatDirection="Horizontal"
SelectedValue = '<%# Bind("Rating") %>'
>
<asp:ListItem Value="0">No Rating</asp:ListItem>
<asp:ListItem Value="1">Poor</asp:ListItem>
<asp:ListItem Value="2">Fair</asp:ListItem>
<asp:ListItem Value="3">Good</asp:ListItem>
<asp:ListItem Value="4">Excellent</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Update Choices" />
<br />
So far? This has been near all drag + drop. As noted, I used the RadioButton list builder like this:
Then I added my choices:
THEN as noted, I cut+ paste that radio button list into the grid as per above.
Now, in my on-load, I have this code:
If IsPostBack = False Then
Dim strSQL As String = "SELECT TOP 10 ID, HotelName, City, Rating from tblHotels ORDER BY HotelName"
Dim cmdSQL As New SqlCommand(strSQL, New SqlConnection(My.Settings.Test3))
cmdSQL.Connection.Open()
GridView1.DataSource = cmdSQL.ExecuteReader
GridView1.DataBind()
GridView1.Columns(0).Visible = False
cmdSQL.Connection.Close()
Session("cmd") = cmdSQL
End If
And now I get this:
Think about how easy this was.
I only so far written about 7 lines of code.
Now the user can change/choose the radio button settings.
How do I now update the rows BACK to the database?
Well, the update button will look like this:
Dim dr As GridViewRow
Dim cmdSQL As SqlCommand = Session("cmd")
cmdSQL.Connection.Open()
Dim rst As New DataTable
rst.Load(cmdSQL.ExecuteReader)
For Each dr In GridView1.Rows
Dim rbL As RadioButtonList = dr.FindControl("RadioButtonList2")
rst.Rows(dr.RowIndex).Item("Rating") = rbL.SelectedValue
Next
Dim daUpdate As New SqlDataAdapter(cmdSQL)
Dim cmdUpdate As New SqlCommandBuilder(daUpdate)
daUpdate.Update(rst)
cmdSQL.Connection.Close()
'cmdSQL.Dispose()
So, that will send the choices back to the database.
Again, look at how little code and effort this was.
So, you might want to expand on WHAT you want to do after a user makes a choice in that radio button. So you want to see/check the values?
Do you want to send the choices back to a database?
As you can see in above, we really don't care (or need) some event for each radio button list.
And if this is some kind of survay system? Then again I would drive the choices from a database. If there are say 5 answers or whatever, then display the results as per above.
And remember, be it a listview, gridview, or even a block of controls as a "repeater"?
Then the above code pattern is quite much the same.
Now, in above I can/could FIRE the row event for a given choice in the radio button list - but at this point in time I am not convinced that this is actually a requirement - but you can get/grab that event if you wish, and take action on each row and as a result of selecting the radio button list (changing values).
EVEN if you goal is not to update a datebase and have a "N" (un-known) list of choices, some type of data grid or list view or data repeater will slice and dice this problem with great ease - and as you can see?
I wrote VERY little code. And I let VS and the form designer write 99% of my markup for me - there was little hand coding of the mark-up - I let the designers and wizards do all the dirty work for me.

gridview column boundfield error

Inside GridView, then inside columns I have a bound field
<asp:BoundField DataField="Company Name" HeaderText="Company Name" SortExpression="Name" />
This displays a list of columns of the companys name (which is fine) however the headerText, is a clickable link that throws an error...how can i get the headertext just to display as a normal plan unclickable label
ta
You can use AllowSorting="False" to the gridview.
<asp:GridView AllowSorting="False">
<asp:BoundField DataField="Company Name" HeaderText="Company Name"/>
</asp:GridView>
Probably because you marked the bound field to be sortable and you didn't implement the sort on the server side... probably. Remove the SortExpression and see what's happening. You should post more info starting with the exception you have.
just remove the SortExpression

Setting a dictionary as datasource

I have the following dictionary and like to set this as the datasource to my Gridview in my asp.net page
Dim myList As New Dictionary(Of Person, string)
the dictionary key is an object of Person which has Id,FirstName and LastName.
and I like to bind the above dictionary to a gridview which shows First name, Last name and Address
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="txtFName" runat="server" Text='<%# Bind("FirstName")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
the next column will be last name and the third will be the address - which is the value property of dictionary
is it possible to bind the above dictionary to a gridview at all?
Thank you.
Your Dictionary is made up of KeyValuePair elements, which exposes two properties: Key and Value.
Since you want to bind items that are accessible through the Key property of each Dictionary entry, you should reference these properties in your databinding as Key.FirstName, Key.LastName, Key.Address, etc.
So given your example above, binding the TextBox should look like:
<asp:TextBox ID="txtFName" runat="server" Text='<%# Bind("Key.FirstName")%>'></asp:TextBox>

How to bind one column to gridview which is not present in database

How to bind one column to the gridview which is not present in the database?
I want to display the total unit in the last column named Total Unit but it is not present in database.
I got an argument exception:
Column 'tunit' does not belong to
table.
foreach(DataRow row in dt.Rows )
{
object[] obj=new object[2];
obj[0] = row["Transaction_Id"];
obj[1] = row["tunit"];
dtgrid.Rows.Add(obj);
}
The best solution to this problem is to implement unbound column as it is explained in the Unbound Columns topic.
if you are binding a list, you can add a property to that list and bind to the grid using eval("PropertyName")
When you prepare the select query make sure you have one more extra column with the total value you want for that particular row, then you can specify a column in the gridview as follows, make sure you specify
'AutoGenerateColumns = false;
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="sds_datasourceName">
<Columns>
<asp:TemplateField HeaderText="Total" >
<itemtemplate>
<asp:Label ID="Total" runat="server" Text='<%# Bind("Total") %>'></asp:Label>
</itemtemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Display image in gridview depending on value of column

I have a gridview which displays rows and columns all linked to an sql statement... and all works as normal.
I want to include a new column, that displays an image depending on what the value of the column is. If the column has a value - it will display an image. If the column value is null no image whill be displayed.
My sql is something like:
SELECT c.call_id, title, a.call_id as b_attach
FROM calls c
LEFT JOIN attachments a ON c.call_id = a.call_id
GROUP BY c.call_id,title,description, a.call_id
What's returns from this sql is:
Call_id | title | b_attach
1235 | title goes here | 1235
1382 | another title |NULL
So if there's something in b_attach - diplay image in gridview column, else display nothing in gridview column
My Gridview:
<asp:HyperLinkField SortExpression="call_id" HeaderText="Call id" DataTextField="call_id"
DataNavigateUrlFields="call_id" DataNavigateUrlFormatString="showcall.aspx?id={0}" />
<asp:HyperLinkField SortExpression="Title" HeaderText="Title" DataTextField="title"
DataNavigateUrlFields="call_id" DataNavigateUrlFormatString="showcall.aspx?id={0}" />
</Columns>
Any ideas on how to do this?
You can use a TemplateField, and, inside it something like this:
<asp:Image runat="server" id="myImg" ImageUrl='<%# GetImage(DataBinder.Eval(Container.DataItem, "b_attach")) >%' visible='<%# null != DataBinder.Eval(Container.DataItem, "b_attach") %> />

Resources