DropDownList in editable DataGrid

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:

<asp:DataGrid 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>
<asp:DropDownList id="NameEdit"
DataValueField="PersonID" DataTextField="Name" DataSource="<%# TempDataView
%>" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

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;
}
 
S

Stephan Bour

Sorry, no change. I need to amend the original statement though. The
datagrid is now reloaded as is instead of disappearing. The "Edit" link is
still present and does not change to the expected Update and Cancel links.

Try calling the PopulateDropDownList () method after setting the
EditItemIndex

Stephan Bour said:
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:

<asp:DataGrid 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>
<asp:DropDownList id="NameEdit"
DataValueField="PersonID" DataTextField="Name" DataSource="<%# TempDataView
%>" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top