Hebron George
Hebron George

Reputation: 349

C# Gridview cell returning empty string

Here's a picture of the gridview I'm working with: https://i.sstatic.net/Ce5gG.png

Here's my issue: I have a DropDownList (not shown in the picture) that has the same number of items as the Fraction DropDownList (shown in image). Each of the indices match with each other (ex: index 4 of the other field is the same as index 4 of the Fraction DropDownList), so when submitting the form, they have to have both of these match properly.

What I want to do is check and see if they do indeed match. I've tried:

this.gvRecords.Rows[0].Cells[2].Text

&

(((DropDownList)this.gvReserveRecords.Controls[0].Controls[0]
.FindControl("gvcbnFraction")).SelectedIndex

Neither have worked. They are just returning an empty string.

Here's some info that might help:

Let me know if you need any other information!

Upvotes: 1

Views: 948

Answers (1)

Egor
Egor

Reputation: 1660

You should be able to simply reference your DropDown control by its ID, in this case - gvcbnFraction. So, gvcbnFraction.SelectedIndex should give the integer index and gvcbnFraction.SelectedValue should give the string value. If you aren't able to reference the drop down control in your code behind, then you are missing something that should be there. Possibly one of these:

  1. Your designer.cs file doesn't include protected global::System.Web.UI.WebControls.DropDownList gvcbnFraction;
  2. Your .aspx file doesn't include CodeBehind="MySuperAwesomePage.aspx.cs" Inherits="MyWebApp._Default" or something similar to that
  3. Something else I just don't know about

If you are using Visual Studio, you can try dragging a control from the toolbox and see if you can reference it. Doing this will automatically update your designer.cs file, eliminating #1

Upvotes: 1

Related Questions