Sajid Hussain

Sajid Hussain

  • 1.2k
  • 514
  • 100k

how to get values from grid view

Sep 15 2015 5:25 AM
i am using following code to generate gridview.
Hide Expand Copy Code
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 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: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
Hide Copy Code
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; }

Answers (1)