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;
}
 
Joined
May 16, 2006
Messages
2
Reaction score
0
I've just encountered the same problem. I've a simple datagrid which lists records from a table and includes a link for edit. The edit procedure should
do a response.redirect, but instead the datagrid disappears.

I've done this many times in the past without a hiccup, so I must have done something different this time. In many cases I copy an existing page to a new name and change the class and the datagrid name, so it's possible this may be causing some confusion. If/When (I hope!) I've sussed it I'll post back.

Cheers
Jim
 
Joined
May 16, 2006
Messages
2
Reaction score
0
OK.. in this particular case, the EnableViewState was set to False
(in the HTML datagrid definition).
Didn't know it was set to True in the first place!
Hopefully this will fix your problem.
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top