8

I have created a few DropDownLists that get populated with data from a database. The first lines of the Dropdownlists are ListItem:

<asp:DropDownList ID="ddlChange_Requestor" runat="server" AppendDataBoundItems="True"  CssClass="ddlChange_Requestor">
    <asp:ListItem>Change Requestor</asp:ListItem>

enter image description here

I also have a GridView that has a RowCommand event on Select button.

enter image description here

When I press Select the DropDownLists will get back whatever value that the respected column/row has:

protected void gwActivity_RowCommand(object sender, GridViewCommandEventArgs e)
{
    {
        GridViewRow row = ((e.CommandSource as Control).NamingContainer as GridViewRow);
        txtActivity.Text = row.Cells[2].Text;
        ddlChange_Requestor.SelectedValue = row.Cells[10].Text;
    }
}

This works when Change Request column/row in the GridView has value but not when it's "white-space" / "Empty" / "Null". I do not really know how to fix it?

I would like to be able to do something like:

ddlChange_Requestor.SelectedValue = isnullOrwhitespace(row.Cells[10].Text , "Change Requestor");

However I would only want this in the background because I would like to have empty row in GridView but on RowCommand Select should understand that empty means ListItem value.

Is this possible?

5377037
  • 9,493
  • 12
  • 43
  • 73
Nils
  • 444
  • 3
  • 8
  • 22

2 Answers2

6

Would it not simply be checking if the value in Cell 10 is empty?

if (!string.IsNullOrEmpty(row.Cells[10].Text) && row.Cells[10].Text != "&nbsp;")
{
    ddlChange_Requestor.SelectedValue = row.Cells[10].Text;
}
else
{
    ddlChange_Requestor.SelectedIndex = 0;
}

Assuming ddlChange_Requestor is a DropDown outside the GridView.

And if you are unsure if the cell value exists in the DLL, you can do a check first.

if (!string.IsNullOrEmpty(row.Cells[10].Text))
{
    string value = row.Cells[10].Text;

    if (ddlChange_Requestor.Items.Cast<ListItem>().Any(x => x.Value == value))
    {
        ddlChange_Requestor.SelectedValue = value;
    }
    else
    {
        ddlChange_Requestor.SelectedIndex = 0;
    }
}
VDWWD
  • 32,238
  • 19
  • 56
  • 70
  • Hi, The first example did not work i still get error: row.Cells[10].Text = " " – Nils Apr 04 '18 at 10:59
  • The second example did work however if i have a value already selected in change requestor like "John" then the DDL won´t get back to default value of "change requestor" but rather stay as "John"? – Nils Apr 04 '18 at 11:00
  • And yes the DDL is outside the gridview – Nils Apr 04 '18 at 11:01
  • I've updated my answer. But figuring out how you might need to tweak the code a little yourself would come in handy in future programming problems. – VDWWD Apr 04 '18 at 11:05
  • Thank you this works :). i'm a "newbie" and trying my best and sometimes even i get stuck and need some help. – Nils Apr 04 '18 at 11:08
  • 2
    No problem. However keep in mind that working with cell values like that can give weird results. It is not a problem with string values, but when it comes to dates and decimals it is best to work with the source data since those types are converted to a string in the Cell Text. – VDWWD Apr 04 '18 at 11:29
-1
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({
          google_ad_client: "ca-pub-2343935067532705",
          enable_page_level_ads: true
     });
</script>
Rook
  • 5,125
  • 3
  • 31
  • 37