I've been trying to teach myself, with no prior web development experience, to design a program for my internship. In order to get familiar with telerik's tools, I've been making practice applications and everything was going fine, until I ran into a snag.
For quite some time now, I've been trying to figure out how to use the dropdownlists in Radgrid to my advantage. I would like a dropdownlist to reference another table, but from, of course, the same database. For instance, say I make a table using the provided telerik.mdf database. I display from the appointments table many fields, including the userid, and would then like to display, based on that userid, the corresponding username from the "users" table. I hope that I'm articulating my needs well enough- I'm new at this.
I have tried a number of things. I have tried, on the properties menu, trying to get the dropdownlist to reference another database, but then datafield never recognizes fields from that second datasource, but if I reference the first datasource, the listtextfield and listvaluefield can't see things from the second table. Maybe theres something I'm missing here about how it works, but I felt like no combination would work here.
I have also tried manually populating the dropdownlist from the codebehind, but the list always ends up empty. I try filling up a dataset with a dataadapter and then giving the dropdownlist the information that way, but it never fills, whether I try to set the properties that way or grab the elements from the dataset individually and put them in- I'm just always ending up with empty lists.
Obviously 2 things are true- I have changed the code around many times and this is just a snapshot, so I hope all of my explanations have articulated well enough the other things I have tried. Furthermore I bet the code is poorly written at this point- I'm still just experimenting with how these things work. Nonetheless, here is the aspx code:
<%
@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AgainWithTheInternStuff._Default" %>
<%
@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" >
<
head runat="server">
<title></title>
<style type="text/css">
.style1
{
margin-top: 0px;
}
</style>
</
head>
<
body>
<form id="form1" runat="server">
<div>
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateEditColumn="True"
CssClass="style1" DataSourceID="SqlDataSource1" GridLines="None"
onitemdatabound="RadGrid1_ItemDataBound">
<
MasterTableView autogeneratecolumns="False" datakeynames="ID"
datasourceid="SqlDataSource1">
<
RowIndicatorColumn>
<
HeaderStyle Width="20px"></HeaderStyle>
</
RowIndicatorColumn>
<
ExpandCollapseColumn>
<
HeaderStyle Width="20px"></HeaderStyle>
</
ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="ID" DataType="System.Int32" HeaderText="ID"
ReadOnly="True" SortExpression="ID" UniqueName="ID"
EditFormColumnIndex="3">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Subject" HeaderText="Subject"
SortExpression="Subject" UniqueName="Subject">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Start" DataType="System.DateTime"
HeaderText="Start" SortExpression="Start" UniqueName="Start">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="End" DataType="System.DateTime"
HeaderText="End" SortExpression="End" UniqueName="End">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="UserID" HeaderText="UserID"
SortExpression="UserID" UniqueName="UserID" DataType="System.Int32">
</telerik:GridBoundColumn>
<telerik:GridDropDownColumn DropDownControlType="DropDownList"
HeaderText="User Name" UniqueName="DropDown">
</telerik:GridDropDownColumn>
</Columns>
</
MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TelerikConnectionString %>"
SelectCommand="SELECT [ID], [Subject], [Start], [End], [UserID] FROM [Appointments]">
</asp:SqlDataSource>
</form>
</
body>
</
html>
And here is the .cs code:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data.SqlClient;
using
System.Data;
using
System.Configuration;
using
Telerik.Web.UI;
namespace
AgainWithTheInternStuff
{
public partial class _Default : System.Web.UI.Page
{
SqlDataAdapter sdadapter ;
DataSet dset ;
SqlCommand scom ;
SqlConnection scon ;
protected void Page_Load(object sender, EventArgs e)
{
scon =
new SqlConnection(ConfigurationManager
.ConnectionStrings[
"TelerikConnectionString"].ConnectionString );
scom =
new SqlCommand();
dset =
new DataSet();
sdadapter =
new SqlDataAdapter();
}
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
scon.Open();
scom.Connection = scon;
scom.CommandText =
"SELECT ID, UserName FROM Users";
sdadapter.SelectCommand = scom;
sdadapter.Fill(dset);
scon.Close();
if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
{
GridEditFormItem efi = (GridEditFormItem)e.Item;
DropDownList ddl = (DropDownList)efi["DropDown"].Controls[0];
ddl.DataSource = dset;
ddl.DataMember =
"Users";
ddl.DataValueField =
"ID";
ddl.DataTextField =
"UserName";
ddl.DataBind();
}
}
}
}
Anyway, I see that you all get back to people very quickly and very thoroughly, and wondered if you could shed some light on why some of these things aren't working, and where my lapse in understanding is. Thank you so much for your time.
Nicole