Gridview

Gridview

gridview start with 0 position


syntax:

string location = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\college.mdf;Integrated Security=True;User Instance=True";

        cn = new SqlConnection(location);

        cn.Open();

        //how to bind grid view 

        string k = "select * from student";

        cm = new SqlCommand(k, cn);

        dr = cm.ExecuteReader();

        GridView1.DataSource = dr;

        GridView1.DataBind();

        dr.Close();


Template in grid view 

   1.template id used to inserting control in a grid view 

   2.Only one property is used to template field 

   


Template field- Header text

grid view select- edit column- template field - header text (student mark)


How to insert control

 in this concept we have to used items template tag


Imp

Grid view crude operation

  1.if control create outside the grid view then we have to used ( for each )

  2.If control inside the grid view then we have to use ( Naming container )


**For each loop in grid view 

1.for each loop read grid view row only 

     

How to create for each

   for each(gridviewrow objname in gridview1.rows)

   {


   }

inside the for each loop find all control in a gridview 


syntax

       control objname=(control)rowobject.findcontrol("controlid");


Example of for each loop :


           //using for each loop

        //foreach(gridviewrow objname in gridview1.rows)


        foreach (GridViewRow r1 in GridView1.Rows)

        {

            //control objname = (control)rowobject.findcontrol("controlid");


            TextBox t1 = (TextBox)r1.FindControl("Textbox1");

            Response.Write(t1.Text);




  • Naming container

               if control inside the grid view then use naming container 


  • Auto generate serial no in grid view  

               if we have to use seial no in a gridview without database then we have to use container concept inside the template field    


syntax:

<asp:TemplateField HeaderText="serialno"> 

                <ItemTemplate>  

                <%# Container.DataItemIndex+1 %> 

                </ItemTemplate> </asp:TemplateField>


  • Dynamic databinding with control

        dynamic binding is a process where database column is directly communicate to control inside the gridview


1.using dynamic binding we have to use Eval();

2.Eval() is communicate to text property


syntax:

      <%# Eval("column") %>

<%# Eval("roll") %>


  • Gridview image concept

 1.Image field

     

  • Property of image field

1.dataimageurlfield -Name of the column

2.Dataimageurlformatstring -Name of the folder


  • Image path:- ~/upload/{0} (0 representing the first folder)


  • How to set image attribute

               <controlstyle>


  • Gridview Paging

1.using allow page option-(true)

2.display 10 record in each page

3.using onpageindexchanging event in gridview

4.create pageindexchanging event in a code


Example:

protected void page_change(object sender, GridViewPageEventArgs e)

    {

    }


  • how to read bulk read

1.if we have to use multiple record at a time this is called bulk concept

2.data reader only read one record at a time 

3.Reading bulk record in a database we have to use sqldataadaptor and data set


Note:In adaptor cannot use command object(cm)


  • How to active page
             use pageindexchanging event

syntax:

GridView1.PageIndex = e.NewPageIndex;

        display();


  • Data repeater 

   1.in repeater display data in row format

   2.repeater is similar to use grid view property(data source or data bind)

   3.Repeater cannot use click event if we have to use event programming in repeater then using command name property of a control (CommandName="btn_display")

   4.use ('<%# Eval("id")%>')


  • Example of Repeater:

        string k = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\testdb.mdf;Integrated Security=True;User Instance=True";

        cn = new SqlConnection(k);

        cn.Open();

        string k1 = "select * from demo";

        cm = new SqlCommand(k1, cn);

        dr=cm.ExecuteReader();

        Repeater1.DataSource=dr;

        Repeater1.DataBind();

        dr.Close();


  • how to active command name

 activate the onitemcommandevent in repeater (OnItemCommand="rep1_command">)


  • How to match command name

                //how to match command name

        if (e.CommandName == "btn_show")

        {

            //how to find control

            Label l1 = (Label)

                e.Item.FindControl("Label1");

            Response.Write(l1.Text);

        }


  • Detailview control

  1. detailview control is used to display information in detail format.
  2. Apply the event handling in detailview control


  • Datalist control

  display record in E-commerce view


there are two property in datalist

 1.Repeat column 

 2.Repeat direction


  • scaller(sum,avg,min)

data control(a.grid view(basic binding,template field,for each loop,naming container,paging,auto generate field, repeater control)


  • how to handle repeater event

1.In repeater we have to use onitemcommand event 

2.In repeater using commandname in place of onclick event


  • command event

   command name is to be activates inside the onitemcommnand event


how to create on item command event in c#


  • how to find control in a repeater

      insert textbox after submit button and update textbox data in name field     


  • Detailview control

 In this control display data in detail format


1.Detailview also use onitemcommand event

2.In Detailview control is used to commandname event

3.Update texbox data in database

4.detailview only display one record at a time


  • how to pass parameter in class

   public string display(string name)

    {

        string n1 = name;  //pass local variable to n1

        return n1;

    }

(string name):local 



Comments

Popular posts from this blog

Basic Query program(insert,update,delete,select)

Data transmission and query string

Detailview(program)