By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

DropDownList in editable DataGrid

P: n/a
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;
}

Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Try calling the PopulateDropDownList () method after setting the
EditItemIndex

"Stephan Bour" <sb***@niaid.nih.gov> wrote in message
news:BC453055.EB30%sb***@niaid.nih.gov...
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;
}

Nov 18 '05 #2

P: n/a
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.

in article #a**************@tk2msftngp13.phx.gbl, Janaka at
ja****@magicalia.com wrote on 2/3/04 12:02 PM:
Try calling the PopulateDropDownList () method after setting the
EditItemIndex

"Stephan Bour" <sb***@niaid.nih.gov> wrote in message
news:BC453055.EB30%sb***@niaid.nih.gov...
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;
}



Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.