Click here to Skip to main content
15,120,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Iam trying to access a dropdownlist inside grideview but i alaways get null exeption

1-the aspx file
<pre><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Employee_Mangment.Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" OnRowDeleted="GridView1_RowDeleted" DataKeyNames="ID" OnRowUpdated="GridView1_RowUpdated" OnRowDataBound="GridView1_RowDataBound">
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                    <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" ReadOnly="true" />
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:TemplateField HeaderText="Education" SortExpression="Education">
                            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource2" DataTextField="EducationType" DataValueField="EducationType" SelectedValue='<%# Bind("Education") %>'>
                            <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetEducations" TypeName="Employee_Mangment.Data.EducationDataAccess"></asp:ObjectDataSource>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Education") %>'></asp:Label>
            <asp:Label ID="Label1" runat="server" Text="" Font-Bold="true"></asp:Label>
            <asp:Label ID="Label2" runat="server" Text="" Font-Bold="true"></asp:Label>
            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetEmployees" TypeName="Employee_Mangment.Data.EmployeeDataAccess" UpdateMethod="UpdateEmployee" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="Original_{0}" DeleteMethod="DeleteEmployee" OnDeleted="ObjectDataSource1_Deleted" OnUpdated="ObjectDataSource1_Updated">
                    <asp:Parameter Name="Original_ID" Type="Int32" />
                    <asp:Parameter Name="Original_Name" Type="String" />
                    <asp:Parameter Name="Original_Education" Type="String" />
                    <asp:Parameter Name="Original_ID" Type="Int32" />
                    <asp:Parameter Name="Original_Name" Type="String" />
                    <asp:Parameter Name="Original_Education" Type="String" />
                    <asp:Parameter Name="Name" Type="String" />
                    <asp:Parameter Name="Education" Type="String" />


2-the code for the accessing is
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    if(e.Row.RowType == DataControlRowType.DataRow)
          DropDownList drop =


What I have tried:

i tried to access it in the
using this method row proparty byt get null too
Updated 1-Apr-20 6:32am
Richard MacCutchan 31-Mar-20 10:23am
Use the debugger to see what is in GridView1.Rows[1]. it may be that the GridView has not been initialised.
Member 14479161 31-Mar-20 15:07pm
getting null too
Richard MacCutchan 1-Apr-20 3:00am
What does that mean?
Member 14479161 1-Apr-20 6:07am
null exeption for the value that i attached GridView1.Rows[1] to it

Richard MacCutchan 1-Apr-20 6:37am
There is still no point in me trying to figure out what that is referring to. Do what I suggested yesterday and use your debugger to find out which actual item is null. Then work backwards to find out why it has not been initialised.
Maciej Los 31-Mar-20 11:19am
Try this (inside GridView1_RowDataBound method):
DropDownList drop = (DropDownList)e.Row.FindControl("DropDownList1");

Let me know if it was helpful.
Member 14479161 31-Mar-20 15:01pm
getting null too

1 solution

The list control is in the EditItemTemplate. Therefore, it will only exist if the row is in the edit state.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) != 0)
        DropDownList drop = (DropDownList)e.Row.FindControl("DropDownList1");
GridViewRow.RowState Property (System.Web.UI.WebControls) | Microsoft Docs[^]
Maciej Los 1-Apr-20 12:13pm
I didn't make enough attention...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900