ComboBox's Selected Value Changed On Lost Focus in VB.NET - vb.net

I have a datagridview(dgv) with a DataGridViewComboBoxColumn(colLocation)
colLocation.AutoComplete = False
colLocation.HeaderText = "Stored to"
colLocation.DataSource = DB.getLocation()
colLocation.DisplayMember = "description"
colLocation.ValueMember = "id"
I added the colLocation to dgv.
"descirption" contains Unicode characters. I can see the comboBox correctly and choose the item.
The problem is when the comboBox lost the focus, the value is changed to first item of the comboBox.
Any suggestion?
Updated:
I found out that the ComboBox doesn't change the data when the DisplayMember is in English characters.
It changes only when the DisplayMember is in Unicode chracter. Any idea for how could solve this? – tunwn 0 secs ago

Your .aspx page should have something like so:
<asp:DropDownList Width="90px" ID="ddlExpenseTypes" OnSelectedIndexChanged="ddlExpenseTypes_SelectedIndexChanged" DataSource='<%# GetExpenseTypes() %>' SelectedValue='<%# Bind("ExpenseReasonID") %>' DataTextField="ExpenseReasonID" DataValueField="ExpenseReasonID" AutoPostBack="true" runat="server" ></asp:DropDownList>
That is you need a functional call for index changed. A function call for filling the drop down ist (DataSource) and a function call to set the SelectedValue of the drop down list.

Is this DataGridView databound? You haven't specified a value for DataPropertyName, so there's nothing for colLocation.ValueMember's ID selection to bind to.

Related

How to get gridview clickable cell data value to new page aspx

I have two columns in a GridView where every cell of 1 column is hyperlinked.
<asp:GridView ID="gvBlogList" runat="server" DataKeyNames="Id"
AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLinkField DataTextField="id"
HeaderText="Blog id" NavigateUrl="detailspage.aspx" />
</ItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
How can I get cell value in 2nd page as a session variable?
Thanks!
As per the what I understand from your question, you want to catch the value of the cell that is clicked, store it in session and make it accessible on the page that have redirect to. This is possible and you can try a flow like this :
You can use the GridView's inbuilt method onRowCommand in this case to generate a server side event and you can then identify which cell was clicked and lastly, you can get its value by index. The below code is a demonstration that you can try and manipulate as per your requirement. This is a sample where a Linkbutton is used in Gridview and on the click of it, its value is fetched.
You can also try to use the CommandName and CommandArgument properties if you want to assign that particular control a value that you may use at the server side.
protected void Grd1_RowCommand(object sender, GridViewCommandEventArgs e)
{
LinkButton lnk = (LinkButton)e.CommandSource;
string value = lnk.Text;
}
And for storing it in Session, you just have to make use of Session["yourVarName"] = value;. And this value can be accessed on the other page.
Hope this helps.

Get DropDownList Selected Value by Selected Text

Do anyone knows how to get selected value of a drop down list by selected text?
I tried the below method but doesn't work:
ddlWorkType.SelectedItem.Text = "writing"
myddlvalue = ddlWorkType.SelectedValue
Please show me how. Thanks.
DDLResp.Items.FindByText(TxtResp.Text.Trim).Selected = True
Try this Code
how to get selected value and selected text in vb.net
<asp:DropDownList CssClass="textbox" ID="ddlUser" runat="server">
<asp:ListItem Value="0">Select User</asp:ListItem>
</asp:DropDownList><br />
Selected DropDownList value
ddlvalue = ddlUser.SelectedItem.Value
Selected DropDownList text
ddlText = ddlUser.SelectedItem.Text
Selected text is actually text which you selecting through mouse drag or by press shift key to copy/paste or delete text. You need to use just .Text instead of .SelectedText
ddlWorkType.Text = "writing"
myddlvalue = ddlWorkType.SelectedValue
if the text does not exist in assign datasource then combobox SelectedIndex will return -1 and SelectedValue will return Nothing.

How to bind the value from textbox to datagrid and datagrid to textbox in vb.net?

i have a datagrid, need to bind the value from textbox to datagrid using add button,
If i double click the datagrid the values want to bind in textbox. after modify the data in text box i need to replace the value for existing data in datagrid.
Like this for textbox to datagrid
<asp:TextBox ID="txtLocation" runat="server" Text='<%# Eval("Location") %>' />

ASP.net VB Listbox

I've seen many questions around this one, but none hitting it directly.
I put a listbox on a page and populate it with three items from an Access database. I have a button on that page that will extract several values including the selected item from the listbox. Or I want to anyway.
I can see the item selected in windows (highlighted) when I click the button, but when I try to select it no item is available as selected in the listbox. The ListBox1.SelectedIndex is alway -1.
Here is the code from the page:
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Text="List1" />
<asp:ListItem Text="List2" />
<asp:ListItem Text="List3" />
</asp:ListBox>
Is there a property missing?
Here is the code from the code behind page:
Public Function getDept() As String
Dim dept As String
If ListBox1.SelectedIndex > -1 Then
dept = ListBox1.SelectedItem.Text
Else
dept = "CMS"
End If
Return dept
End Function
Please help, I have until about noon to figure this out.
There may some reasons:
1- Check if your page's viewstate is true.
2- Call your method after Page_Load event.
Where do you call the function?
Consider that you should call it after Page_Load event. Also your viewstate

'RadioButtonList' has a SelectedValue which is invalid because it does not exist in the list of items

I've a RadioButtonList looks like this:
<asp:RadioButtonList
ID="rblAgenda"
SelectedValue = '<%# Bind("bitAgenda") %>'
runat="server"
RepeatDirection="Horizontal">
<asp:ListItem Value="1">Yes</asp:ListItem>
<asp:ListItem Value="0">No</asp:ListItem>
</asp:RadioButtonList>
This is inside a formview which is bound to a sqlDataSource. I checked the stored procedure it is returning 1 for bitAgenda, but I'm still getting this error message:
'rblAgenda' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
It's driving me crazy >:( Any Idea??? Btw bitAgenda is a bit Field in database, is that what causing the problem?
Try changing your ListItem values from 1 and 0 to true and false respectively.