Friday, 18 May 2012

How to use DetailsView Control in asp.net?


Detailsview is a databound control which is a structured control like Gridview. Which is displays data in Rows format(Gridview Columns format);

Detailsview has more similarities with gridview control. Detailsview and gridview both are shows data in tabular format.

Detailsview shows data in the format as Fields so supporting Fields Collection as well
Gridview shows data in the format as Columns so supporting Collumns Collection.
 
Detailsview Classes,Properties,,Collections:

Detailsview1.AutoGenerateRows=true;
AutoGenerateDeleteButtion,AutoGenerateEditButtion,AutoGenerateCancleButtion….ect.

DetailsViewRow like GridViewRow class.
DetailsViewRowCollection like GridviewRowCollection class.
public virtual DataControlFieldCollection Fields { get; }
public virtual GridViewRowCollection Rows { get; }
…like similar classes in both controls.

Detailview1.Fields.Add(DataControlField filed);
GridView1.Columns.Add(DataControlField filed);…so both are same.other then data representation.

Details view Events:
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"
                    AutoGenerateRows="False" AutoGenerateEditButton="True" DataKeyNames="Empid"                  
                    onmodechanging="DetailsView1_ModeChanging"
                    onitemupdating="DetailsView1_ItemUpdating"                   
                    AutoGenerateDeleteButton="True" AutoGenerateInsertButton="True"
                    onitemdeleting="DetailsView1_ItemDeleting"
                    oniteminserting="DetailsView1_ItemInserting" AllowPaging="True"
                    onpageindexchanging="DetailsView1_PageIndexChanging">                            
                    <Fields>                         
<asp:BoundField DataField="Empname" HeaderText="name" />                                                      
<asp:BoundField DataField="EmpDesignation" HeaderText="design" />            
<asp:BoundField DataField="Empsal" HeaderText="Salaries" DataFormatString="{0:c}" />              
<asp:BoundField DataField="EmpProFund" HeaderText="PF" DataFormatString="{0:c}"/>                                                                        
 <asp:BoundFieldDataField="EmpAddress"HeaderText="Address"/>                                                                
<asp:BoundField DataField="EmpJoindate" HeaderText="jodate" DataFormatString="{0:d}" />                                                                   
 <asp:CheckBoxField DataField="Status" HeaderText="status" />                                      
                    </Fields>
                </asp:DetailsView>



 
CodeBehind
public partial class DetailsViewDemo : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("uid=sa;pwd=zolt123$;database=PropercodeDB");
    DataSet ds;
    SqlDataAdapter da; 
    SqlParameter empname, empdesign, empsal, emppf, empadd, empdoj, empstatus, empid;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {      
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        EmployeeDetails();
    }
    public void EmployeeDetails()
    {
        da = new SqlDataAdapter("select * from EmployeeDetails", con);
        ds = new DataSet();
        da.Fill(ds);
        DetailsView1.DataSource = ds;
        DetailsView1.DataBind();
    }
    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {               
        switch (e.NewMode)
        {
            case DetailsViewMode.Edit:
                DetailsView1.ChangeMode(DetailsViewMode.Edit);
                EmployeeDetails();
                break;
            case DetailsViewMode.Insert:
                DetailsView1.ChangeMode(DetailsViewMode.Insert);
                EmployeeDetails();
                break;
            case DetailsViewMode.ReadOnly:
                DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
                EmployeeDetails();
                break;
            default:
                break;
        }
        
    }

    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
       
        string ss = DetailsView1.DataKey[DetailsView1.DataItemIndex].ToString();
        int eid=Convert.ToInt32(ss);

        TextBox txtname = (TextBox)DetailsView1.Rows[0].Cells[1].Controls[0];
        TextBox txtdesign = (TextBox)DetailsView1.Rows[1].Cells[1].Controls[0];

        TextBox txtsal = (TextBox)DetailsView1.Rows[2].Cells[1].Controls[0];
        TextBox txtpf=(TextBox)DetailsView1.Rows[3].Cells[1].Controls[0];
        TextBox txtaddress=(TextBox)DetailsView1.Rows[4].Cells[1].Controls[0];
        TextBox txtdofjoin=(TextBox)DetailsView1.Rows[5].Cells[1].Controls[0];
        CheckBox chstatus=(CheckBox)DetailsView1.Rows[6].Cells[1].Controls[0];
        cmd = new SqlCommand("Emp_Update", con);
        cmd.CommandType = CommandType.StoredProcedure;

        empname = new SqlParameter("@ename", txtname.Text);
        empdesign = new SqlParameter("@designation", txtdesign.Text);
        empsal= new SqlParameter("@esal", txtsal.Text);
        emppf = new SqlParameter("@epf", txtpf.Text);
        empadd = new SqlParameter("@eadd", txtaddress.Text);
        empdoj = new SqlParameter("@edoj", txtdofjoin.Text);
        empstatus = new SqlParameter("@estatus", chstatus.Checked);
        empid = new SqlParameter("@eid", eid);

        SqlParameter[] epars = new SqlParameter[] { empname,empdesign,empsal,emppf,empadd,empdoj,empstatus,empid };
     
        //foreach (SqlParameter item in epars)
        //{
        //    cmd.Parameters.Add(item);                   // Plz avoid of iteratation
        //}
    
        IEnumerator ier = epars.GetEnumerator();
        while (ier.MoveNext())
        {
            SqlParameter item = (SqlParameter)ier.Current;
            cmd.Parameters.Add(item);
        }
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);                                      
    }

    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {
        DetailsView1.PageIndex = e.NewPageIndex;
        EmployeeDetails();
    }
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        TextBox txtname = (TextBox)DetailsView1.Rows[0].Cells[1].Controls[0];
     
        TextBox txtdesign = (TextBox)DetailsView1.Rows[1].Cells[1].Controls[0];
      
        TextBox txtsal = (TextBox)DetailsView1.Rows[2].Cells[1].Controls[0];
      
        TextBox txtpf = (TextBox)DetailsView1.Rows[3].Cells[1].Controls[0];
     
        TextBox txtadd = (TextBox)DetailsView1.Rows[4].Cells[1].Controls[0];
     
        TextBox txtjod = (TextBox)DetailsView1.Rows[5].Cells[1].Controls[0];
       
        CheckBox cstatus = (CheckBox)DetailsView1.Rows[6].Cells[1].Controls[0];

        cmd = new SqlCommand("Proper_insertemp", con);
        cmd.CommandType = CommandType.StoredProcedure;

        empname= new SqlParameter("@ename", txtname.Text);
        empdesign = new SqlParameter("@designation", txtdesign.Text);
        empsal = new SqlParameter("@esal", txtsal.Text);
        emppf = new SqlParameter("@epf", txtpf.Text);
        empadd = new SqlParameter("@eadd", txtadd.Text);
        empdoj = new SqlParameter("@edoj", txtjod.Text);
        empstatus = new SqlParameter("@estatus", cstatus.Checked);
        //epars[7] = new SqlParameter("@eid", ss.ToString());
        SqlParameter[] epars = new SqlParameter[] { empname, empdesign, empsal, emppf, empadd, empdoj, empstatus};
        IEnumerator ier = epars.GetEnumerator();
        while (ier.MoveNext())
        {
            SqlParameter item = (SqlParameter)ier.Current;
            cmd.Parameters.Add(item);
        }
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        Label1.Text = "Inserted Succefully.....";                      
    }

    protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {
            // like above;

    }
}
 Note: StoredProcedure "Proper_insertemp" is available in Previous article/Demo:
How many Data/Composite controls available in asp.net? 

No comments:

Post a Comment