Asked by:
aspGridView and DropDownList.

Question
-
User1266884534 posted
Hello Folks,
I have a form that has aspGridView control on it. The control is displayed when a search is performed on the page. The issue I am facing is with a <g class="gr_ gr_393 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="393" data-gr-id="393">dropdownlist</g> on this control. The dropdown list is getting populated with all the values, but I am not able to display the <g class="gr_ gr_718 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="718" data-gr-id="718">selectedValue</g>.I have the code to populate the <g class="gr_ gr_808 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="808" data-gr-id="808">dropdownlist</g> control in the RowDataBound event of the <g class="gr_ gr_823 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="823" data-gr-id="823">gridview</g> control. Here is the snippet of code.
The <g class="gr_ gr_897 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="897" data-gr-id="897">selectedValue</g> property assignment fails with the following message -->
'dr["Id"].ToString()' threw an exception of type 'System.NullReferenceException'
protected void gridResults_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { var ddlAccountType = DropDownList)e.Row.FindControl("dlAccountType"); ddlAccountType.DataSource = DataLayer.GetAllAccountTypes(); ddlAccountType.DataTextField = "Type"; ddlAccountType.DataValueField = "Id"; ddlAccountType.DataBind(); DataRowView dr = e.Row.DataItem as DataRowView; ddlAccountType.SelectedValue = dr["Id"].ToString(); } }
Monday, March 23, 2020 5:55 PM
All replies
-
User1266884534 posted
So I had to change part of the code in order to assign the DataItem value correctly. Here is the updated code.
var ddlAccountType = (DropDownList)e.Row.FindControl("dlAccountType"); ddlAccountType.DataSource = DataLayer.GetAllAccountTypes(); ddlAccountType.DataTextField = "Type"; ddlAccountType.DataValueField = "Id"; ddlAccountType.DataBind(); var glaccountTypeId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Id")); ddlAccountType.Items.FindByValue(glaccountTypeId.ToString()).Selected = true;
Monday, March 23, 2020 9:00 PM -
User1535942433 posted
Hi learning_to_code,
Accroding to your description and codes,I suggest you could debug to check wheather dr["Id"] is null.What is your grdiview aspx page and what is your dropdownlist data?
Do you have field of Id in dropdownlist data?I create a demo.
More details,you could refer to below codes:
ASPX:
<div> <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="Id"> <ItemTemplate> <asp:Label runat="server" Text='<%# Eval("Id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Value"> <ItemTemplate> <asp:Label runat="server" Text='<%# Eval("Value") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ddl"> <ItemTemplate> <asp:DropDownList runat="server" ID="dlAccountType"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div>
Code-behind:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } protected void bind() { string str, strSql; str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection conn = new SqlConnection(str); strSql = "select * from gv"; SqlDataAdapter da = new SqlDataAdapter(strSql, str); DataSet ds = new DataSet(); da.Fill(ds, "gv"); GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); conn.Close(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string query = "select Id,Type from gv"; SqlConnection sqlConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString); sqlConn.Open(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(query, sqlConn); DataTable dt = new DataTable(); da.Fill(dt); sqlConn.Close(); var ddlAccountType = (DropDownList)e.Row.FindControl("dlAccountType"); ddlAccountType.DataSource = dt; ddlAccountType.DataTextField = "Type"; ddlAccountType.DataValueField = "Id"; ddlAccountType.DataBind(); DataRowView dr = e.Row.DataItem as DataRowView; ddlAccountType.SelectedValue = dr["Id"].ToString(); } }
Result:
Best regards,
Yijing Sun
Tuesday, March 24, 2020 2:44 AM