Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i want a check box in detaill view for approved and not approved with Sql Server if i checked checkbox it should save in data base as approved or true ,

this link work fine for me for gridview i want it in DetailView ,..

http://www.ezzylearning.com/tutorial.aspx?tid=5187857&q=using-checkbox-in-asp-net-gridview-control

here is my code i am getting error on "Cells"

Error 18 'System.Web.UI.WebControls.DetailsView' does not contain a definition for 'Cells' and no extension method 'Cells' accepting a first argument of type 'System.Web.UI.WebControls.DetailsView' could be found (are you missing a using directive or an assembly reference?) C:\Users\Administrator\Documents\Visual Studio 2010\KSMC_EmpDtl\KSMC_EmpDtl\search.aspx.cs 50 30 KSMC_EmpDtl



C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               LoadData();
           }
       }

       private void LoadData()
       {

           string query = @"SELECT ID,Name,Emp_No,Job_Title,Nationality,Administration,Section,Another_job,health_no,Exp_Date,National_ID,serial_no,reference,confirm_rcrd,arabic_name,arabic_jobtitle FROM KSMCemp_data";

           SqlDataAdapter da = new SqlDataAdapter(query, con);
           DataTable table = new DataTable();
           da.Fill(table);

           GridView2.DataSource = table;
           GridView2.DataBind();

       }

       public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
       {
           CheckBox chkStatus = (CheckBox)sender;
           DetailsView row = (DetailsView)chkStatus.NamingContainer;


           string cid = row.Cells[1].Text;                              <<--- ERROR
           bool status = chkStatus.Checked;



           string query = "UPDATE KSMCemp_data SET confirm_rcrd = @confirm_rcrd WHERE ID = @ID";


           SqlCommand com = new SqlCommand(query, con);


           com.Parameters.Add("@confirm_rcrd", SqlDbType.Bit).Value = status;

           com.Parameters.Add("@ID", SqlDbType.Int).Value = cid;


           con.Open();
           com.ExecuteNonQuery();
           con.Close();

           LoadData();

       }


ASP.NET
<asp:DetailsView ID="DetailsView1" runat="server"
          AutoGenerateRows="False" CellPadding="4"  DataKeyNames="ID"
          ForeColor="#333333" GridLines="Horizontal" Height="50px"
          Width="291px" HorizontalAlign="Left" ViewStateMode="Enabled">
          <AlternatingRowStyle BackColor="White" />
          <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
          <EditRowStyle BackColor="#2461BF" />
          <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
          <Fields>



                  <asp:TemplateField HeaderText="Confirm Records">
                  <ItemTemplate>
              <asp:CheckBox ID="chkStatus" runat="server"
                          AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged"
                          Checked='<%# Convert.ToBoolean(Eval("confirm_rcrd")) %>'
                          Text='<%# Eval("confirm_rcrd").ToString().Equals("True") ? " Confirm " : " Pending " %>'  />
                  </ItemTemplate>
                   </asp:TemplateField>

               <asp:BoundField DataField="ID" HeaderText="Serial No" />
              <asp:BoundField DataField="Name" HeaderText="Name"  />
              <asp:BoundField DataField="Emp_No" HeaderText="Emp_No"
                  />
Posted

1 solution

Isn't the error message quite obvious? There is no such member as Cells in the class System.Web.UI.WebControls.DetailsView.

Your variable naming (row) creates the impression that you somehow mixed up two different classes: System.Web.UI.WebControls.DetailsView and, probably, System.Web.UI.WebControls.DetailsViewRow, which does have the property Cells. Please see:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsviewrow.aspx[^].

Even if you correctly calculated the row reference in the preceding line of code (so the typecast won't throw invalid cast exception), this variable will get a reference to some System.Web.UI.WebControls.DetailsViewRow instance which you can use to reference on of the particular row instances; please see the first MSDN article referenced above.

—SA
 
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