Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am using following code to generate gridview.
C#
protected void Page_Load(object sender, EventArgs e)
     {
         if (!this.IsPostBack)
         {
             this.BindGrid();
         }
     }

     private void BindGrid()
     {
         Entities entities = new Entities();


         Gridview1.DataSource = from customer in entities.SectorWisePoints.ToList()
                                    select customer;

         Gridview1.DataBind();

     }
     //protected void gridView_PreRender(object sender, EventArgs e)
     //{
     //    GridDecorator.MergeRows(Gridview1);
     //}
     protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
     {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {

             CheckBox CHKBOX = (e.Row.FindControl("Released") as CheckBox);
             CheckBox CHKBOX1 = (e.Row.FindControl("Released1") as CheckBox);
                 DropDownList ddlEmployee = (e.Row.FindControl("ddlEmp") as DropDownList);
                 DropDownList ddlEmployee2 = (e.Row.FindControl("ddlEmp2") as DropDownList);
                 var emp1 = _service.GetEmployeeDutyByEmployee_Id(MyUser.Employee_Id).LastOrDefault();
                 var EmpList = _service.GetAllEmployeeDuty().OrderByDescending(x => x.EndDate).GroupBy(x => x.Employee_Id).Select(x => x.First()).ToList();
                 var empList = EmpList.Where(X => X.ToSector_Id == emp1.ToSector_Id).ToList();
                 ddlEmployee.Bind(empList, "EmployeeIdName", "Employee_Id");
                 ddlEmployee2.Bind(empList, "EmployeeIdName", "Employee_Id");





         }
     }

aspx code is
ASP.NET
<asp:GridView ID="Gridview1" ControlStyle-BackColor="#ccccff" CssClass="col-sm-10" runat="server"   AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" 
                        >
                        <Columns>
                            
                            <asp:TemplateField HeaderText="PointName">
                                <ItemTemplate>
                                    <asp:Label ID="lblPointName"  runat="server" Text='<%# Eval("Name")%>'> ></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>    
                            <asp:TemplateField HeaderText="Employee">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlEmp" runat="server" OnSelectedIndexChanged="ddlEmp_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                                </ItemTemplate>
                            </asp:TemplateField>
                             <asp:TemplateField HeaderText="Released">
                                <ItemTemplate>
                                    <asp:CheckBox  runat="server" ID="Released"/>
                                </ItemTemplate>                                
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="PointName">
                                <ItemTemplate>
                                    <asp:Label ID="lblPointName1"  runat="server" Text='<%# Eval("Name")%>'>></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>  
                             <asp:TemplateField HeaderText="Employee">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlEmp2" runat="server" OnSelectedIndexChanged="ddlEmp_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                                </ItemTemplate>
                                
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Released">
                                <ItemTemplate>
                                    <asp:CheckBox  runat="server" ID="Released1"/>
                                </ItemTemplate>
                                
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

i want to save point name and selected employee on this selected event ,but i am unable to gat values,i am trying following code ,its give exception
C#
protected void ddlEmp_SelectedIndexChanged(object sender, EventArgs e)
   {
       DropDownList ddl1 = (DropDownList)Gridview1.FindControl("ddlEmp");
       string Emp1 = ((DropDownList)Gridview1.SelectedRow.FindControl("ddlEmp")).SelectedItem.Value;
       string Emp2 = ((DropDownList)Gridview1.SelectedRow.FindControl("ddlEmp")).SelectedItem.Value;
       string pointname = ((Label)Gridview1.SelectedRow.FindControl("lblPointName")).Text;
       string pointname1 = ((Label)Gridview1.SelectedRow.FindControl("lblPointName")).Text;
   }

[edit]SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 14-Sep-15 23:33pm
v2
Comments
Naveen.Sanagasetti 15-Sep-15 5:33am    
Share your exception details instead of post your full code; that will help you to get more responses from our end.
OriginalGriff 15-Sep-15 5:33am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.

I'm guessing, since you haven't posted the exception details, that you're getting a NullReferenceException when you call GridView1.SelectedRow.FindControl(...); your grid doesn't have a selected row, so you're trying to call a method on a null reference.

Try using the sender parameter to get the parent row, and find your controls from there:
C#
protected void ddlEmp_SelectedIndexChanged(object sender, EventArgs e)
{
    Control row = ((Control)sender).NamingContainer;
    
    DropDownList ddlEmp = (DropDownList)row.FindControl("ddlEmp");
    DropDownList ddlEmp2 = (DropDownList)row.FindControl("ddlEmp2");
    Label lblPointName = (Label)ddlEmp.NamingContainer.FindControl("lblPointName");
    Label lblPointName1 = (Label)ddlEmp.NamingContainer.FindControl("lblPointName1");
    
    string Emp1 = ddlEmp.SelectedValue;
    string Emp2 = ddlEmp2.SelectedValue;
    string pointname = lblPointName.Text;
    string pointname1 = lblPointName1.Text;
    
    ...
}

If that doesn't solve the problem, then you'll need to post the full details of the exception you're getting.
 
Share this answer
 
Comments
Sajid227 16-Sep-15 0:56am    
nice approach bro
Try the given below code-

protected void ddlEmp_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvRow = ((GridViewRow)((DropDownList)sender).NamingContainer);
            DropDownList ddlEmp = gvRow.FindControl("ddlEmp") as DropDownList;
}
 
Share this answer
 

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