S
Stephan Bour
Hi,
I¹ve tried to implement a solution to populate a dropdownlist inside an
editable datagrid. The code is simple enough and an example can be found
here: <http://www.4guysfromrolla.com/webtech/050801-1.shtml>
The problem I'm having is that the entire datagrid disappears after I click
the Edit link.
Could someone tell me what I'm doing wrong?
Much appreciated,
Stephan.
THE GRID:
<aspataGrid id="ByJob" AutoGenerateColumns="false"
OnEditCommand="ByJob_Edit" OnCancelCommand="ByJob_Cancel"
OnUpdateCommand="ByJob_Update" DataKeyField="PersonID" runat="server">
<HeaderStyle backcolor="Black" forecolor="White" font-bold="True"
horizontalalign="Left" />
<AlternatingItemStyle BackColor="#DEDCFF" />
<Columns>
<asp:EditCommandColumn EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
ItemStyle-Wrap="false"
/>
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<asp:Label ID="NameLabel" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList id="NameEdit"
DataValueField="PersonID" DataTextField="Name" DataSource="<%# TempDataView
%>" runat="server"></aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid>
THE CODE BEHIND:
public virtual void Page_Load (Object Sender, EventArgs e) {
if (! Page.IsPostBack) {
Defaults (); //Defaults runs a SQL query that populates the
datagrid. That part works fine (not shown)
}
}
protected void PopulateDropDownList () {
try {
String ShowPeople = "SELECT * FROM People";
SqlConnection myConnection = new SqlConnection("server =
'LMM-MB-DATA,25886';Database = 'LMM_Jobs'; Trusted_Connection=Yes");
SqlDataAdapter myCommand = new SqlDataAdapter(ShowPeople,
myConnection);
DataSet ds_Names = new DataSet();
myCommand.Fill(ds_Names, "People");
DataView TempDataView = new DataView(ds_Names.Tables["People"]);
//DataBind();
}
catch (Exception SQL) {
SqlError.Text = "<strong><center><font color = red>Sorry, there
was a problem with the database connection. Please try again
later</center></strong></font><br>" +
SQL.Message;
}
}
protected void ByJob_Edit (object sender, DataGridCommandEventArgs e) {
PopulateDropDownList ();
ByJob.EditItemIndex = e.Item.ItemIndex;
}
I¹ve tried to implement a solution to populate a dropdownlist inside an
editable datagrid. The code is simple enough and an example can be found
here: <http://www.4guysfromrolla.com/webtech/050801-1.shtml>
The problem I'm having is that the entire datagrid disappears after I click
the Edit link.
Could someone tell me what I'm doing wrong?
Much appreciated,
Stephan.
THE GRID:
<aspataGrid id="ByJob" AutoGenerateColumns="false"
OnEditCommand="ByJob_Edit" OnCancelCommand="ByJob_Cancel"
OnUpdateCommand="ByJob_Update" DataKeyField="PersonID" runat="server">
<HeaderStyle backcolor="Black" forecolor="White" font-bold="True"
horizontalalign="Left" />
<AlternatingItemStyle BackColor="#DEDCFF" />
<Columns>
<asp:EditCommandColumn EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
ItemStyle-Wrap="false"
/>
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<asp:Label ID="NameLabel" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList id="NameEdit"
DataValueField="PersonID" DataTextField="Name" DataSource="<%# TempDataView
%>" runat="server"></aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid>
THE CODE BEHIND:
public virtual void Page_Load (Object Sender, EventArgs e) {
if (! Page.IsPostBack) {
Defaults (); //Defaults runs a SQL query that populates the
datagrid. That part works fine (not shown)
}
}
protected void PopulateDropDownList () {
try {
String ShowPeople = "SELECT * FROM People";
SqlConnection myConnection = new SqlConnection("server =
'LMM-MB-DATA,25886';Database = 'LMM_Jobs'; Trusted_Connection=Yes");
SqlDataAdapter myCommand = new SqlDataAdapter(ShowPeople,
myConnection);
DataSet ds_Names = new DataSet();
myCommand.Fill(ds_Names, "People");
DataView TempDataView = new DataView(ds_Names.Tables["People"]);
//DataBind();
}
catch (Exception SQL) {
SqlError.Text = "<strong><center><font color = red>Sorry, there
was a problem with the database connection. Please try again
later</center></strong></font><br>" +
SQL.Message;
}
}
protected void ByJob_Edit (object sender, DataGridCommandEventArgs e) {
PopulateDropDownList ();
ByJob.EditItemIndex = e.Item.ItemIndex;
}