I have a dropdownlist in template which is bound to a the db:
Then I have a bound column "Project" that I would like to be automatically filled in when a value in the dropdown is selected based on a query:
Select Project from Pnum where Project = dropdown selected value, and save it.
How can I achieve that?
Your help will be appreciated.
Thanks,
Felice
<
telerik:RadDropDownList
ID
=
"RadDropDownList1"
runat
=
"server"
DataSourceID
=
"SqlDataSource2"
DataTextField
=
"Pnum"
DataValueField
=
"Pnum"
SelectedValue='<%#Bind("Pnum") %>'>
</
telerik:RadDropDownList
>
Then I have a bound column "Project" that I would like to be automatically filled in when a value in the dropdown is selected based on a query:
Select Project from Pnum where Project = dropdown selected value, and save it.
How can I achieve that?
Your help will be appreciated.
Thanks,
Felice
4 Answers, 1 is accepted
0
Princy
Top achievements
Rank 2
answered on 14 Apr 2014, 07:41 AM
Hi Felice,
I guess you have the RadDropDownList in the ItemTemplate and you want to change a BoundColumn value based on DropDown selection. Please take a look at the following code snippet.
ASPX:
C#:
Thanks,
Princy
I guess you have the RadDropDownList in the ItemTemplate and you want to change a BoundColumn value based on DropDown selection. Please take a look at the following code snippet.
ASPX:
<
telerik:GridTemplateColumn
>
<
ItemTemplate
>
<
telerik:RadDropDownList
ID
=
"RadDropDownList1"
runat
=
"server"
DataSourceID
=
"SqlDataSource2"
DataTextField
=
"Pnum"
DataValueField
=
"Pnum"
SelectedValue='<%#Bind("Pnum") %>'
OnSelectedIndexChanged="RadDropDownList1_SelectedIndexChanged" AutoPostBack="true">
</
telerik:RadDropDownList
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
C#:
protected
void
RadDropDownList1_SelectedIndexChanged(
object
sender, DropDownListEventArgs e)
{
RadDropDownList droplist = sender
as
RadDropDownList;
GridDataItem item = droplist.NamingContainer
as
GridDataItem;
DataTable dt=GetDataTable(
"SELECT Project FROM TableName WHERE Project='"
+droplist.SelectedItem.Text+
"'"
);
string
value = dt.Rows[0][
"Project"
].ToString();
item[
"Project"
].Text = value;
}
Thanks,
Princy
0
Felice
Top achievements
Rank 1
answered on 14 Apr 2014, 05:18 PM
Hi Princy,
thanks for your support. This is what I did but I am getting an error:
aspx (please note that this is in a form template:
this is my column:
c#:
I get the error " System.IndexOutOfRangeException: No rows at position 0"
on this line: "string value = dt.Rows[0]["Project"].ToString();"
thanks for your support. This is what I did but I am getting an error:
aspx (please note that this is in a form template:
<
telerik:RadDropDownList
ID
=
"RadDropDownList1"
runat
=
"server"
DefaultMessage
=
"Please select a project"
DataSourceID
=
"SqlDataSource2"
DataTextField
=
"Pnum"
DataValueField
=
"Pnum"
SelectedValue='<%#Bind("Pnum") %>' Skin="Outlook" OnSelectedIndexChanged="RadDropDownList1_SelectedIndexChanged" AutoPostBack="true">
</
telerik:RadDropDownList
>
this is my column:
<
telerik:GridBoundColumn
DataField
=
"Project"
FilterControlAltText
=
"Filter Project_column column"
HeaderText
=
"Project"
UniqueName
=
"Project"
>
<
ColumnValidationSettings
>
<
ModelErrorMessage
Text
=
""
/>
</
ColumnValidationSettings
>
<
HeaderStyle
Width
=
"120px"
/>
</
telerik:GridBoundColumn
>
c#:
protected void RadDropDownList1_SelectedIndexChanged(object sender, DropDownListEventArgs e)
{
RadDropDownList droplist = sender as RadDropDownList;
GridDataItem item = droplist.NamingContainer as GridDataItem;
DataTable dt = GetDataTable("SELECT Project FROM Pnum WHERE Project='"+droplist.SelectedItem.Text+"'");
string value = dt.Rows[0]["Project"].ToString();
item["Project"].Text = value;
}
public DataTable GetDataTable(string query)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["WTAConnectionString"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
return dt;
}
I get the error " System.IndexOutOfRangeException: No rows at position 0"
on this line: "string value = dt.Rows[0]["Project"].ToString();"
0
Felice
Top achievements
Rank 1
answered on 14 Apr 2014, 07:48 PM
Hi Princy,
I sorted out the problem about my previous post.
However now I am getting an error on this line and I do not know how to fix this:
item["Project"].Text = value;
An object reference not set to an instance of an object.
Thanks for helping.
Felice
I sorted out the problem about my previous post.
However now I am getting an error on this line and I do not know how to fix this:
item["Project"].Text = value;
An object reference not set to an instance of an object.
Thanks for helping.
Felice
0
Princy
Top achievements
Rank 2
answered on 16 Apr 2014, 03:54 AM
Hi Felice,
I'm not able to replicate the issue at my end. The code works fine at my side. Please provide your full code snippet to try to replicate the issue.
Thanks,
Princy
I'm not able to replicate the issue at my end. The code works fine at my side. Please provide your full code snippet to try to replicate the issue.
Thanks,
Princy