Tuesday, 28 October 2014

GRID VIEW CONTROLS

Note: -In .NET 1.0 we had data grid view control that was replaced in 2.0 with grid view.
         -Data grid 1.1 control available in 2.0.
         -Grid view is faster in performance.
         -it also had a simplified use age.
         -Dynamic nature.
         -Have new features
*Note: We cannot drag and drop a data grid but we have to go to the code and create.
        Data grid in ASP.NET 2.0 can be created just by writing data grid control creation code in source view
        (because there is no drag and drop provided by default)
        Ex: <asp: Data Grid id=”dg1”runat=”server”/>
Grid view features
       -Most featured rich control.
       -Many built-in layouts for designing &dynamic behavior.
       -Supports sorting of data.
       -Supports Paging of data.
       -Supports Editing/Updating/Deleting of data.
       -Collection based approach is provided.
       -Grid view with auto generates columns true on properties that generate columns dynamically.
          When data source is specified at run time and it is by default is TRUE.
       -we can assign different data objects tables at runtime to provide different results.
       - If auto generate columns is set to false then we need to provide columns on our own.
       -With auto generate columns true we can really provide some4 automated sorting /paging tasks
          also.
       -Grid view when assigned to data source control it automatically generates the code that is
          required to show data. It internally sets automate column is set to false.
       -Enable sorting, enable paging.
       -Grid view with data source controls –providing additionally sorting paging.


Bound Controls
                (i)Data Object
                (ii)Design
                (iii)Bind
Steps to create data Grid
(i)Create a data grid view
(ii)Take a button
(iii)Double click on the design screen to go to the code view.
(iv)Grid view has capability to render itself.
                                                                                     COLUMNS: (FALSE)
- When auto generate columns is set to false we have to explicitly provide the columns information.


-

Using predefined fields of grid views like
<asp: Check Box Field…..> (Not normal check box)
<asp: image Field….> (not normal image).
METHOD-3:
Note: Grid view present data tabular presentation purely in the form of rows and columns.
USING BOUND FIELD
 Process: Create button Keep auto generate columns “False” Drag and drop Grid view Go to source
view Use <column><asp: Bound Field…>to define all columns Inside Grid View write this code
                      <asp: Grid view id=”gridview1” runat=”server”>
(//open column//) <Column>
                      <asp: Bound Field Data Field=”Job_id” Header Text=”job_id”/>
                      <asp: Bound Field Data Field=”job_desc”Header Text=”job_desc”/>
                      <asp: Bound Field Data Field=”min_lvl” Header Text=”Min_lvl”/>
                      <asp: Bound Field Data Field=”Max_lvl” Header Text=”Max_lvl”/>
                      </Column>
//Then give the server connection Code in the” Code view window”//
Ex: Using System. Data.SqlClient.
     (The data connection code)
      {
        Sql.Connection cn=new Sqlconnection ("user id=sa; database=”… “; data source=”server");
        cn. Open ( );
        Dataset ds=new Dataset ( );
        Sql Data Adaptor da=new SqlData Adaptor (“select * from “table name”);
        Data Fill (ds,”tablename”);
        Grid view 1.Data source=ds.table [0];
        Grid view 1.Data Bind ( );
        }
**Note:
-Bound Field is better and also only used to display some static data like Data Object Field.
-If we want to display any ”Check Box” ,”Link Button”, “Button” or others and provide action to it then
  we would prefer to use template field.
-Template Field has templates that allow user to define own templates or some custom data .it can be used like a bound
field also.
*Note*:
10

-One Template Field means one column, even though there are 100 columns in Template Field.
Ex:
    <asp: Template Field>
    <Header Template>select</Header template>
    <Item Template>
    <asp: Label id=”lbl 1”text=’<%#Eval (“job-id”) %>’runat=”server”></asp: Label>
    </Item Template>
    <Item Template>
    <asp: Listitem>interested</asp: ListiItem>
    <asp: List item>notintrested</asp: Listitem>
    <asp: List item>buy later</asp: List item>
    </asp: Drop Down List>
    <Item Template>
-To create a data Grid view to Update, Edit, Cancel the database tables we have to follow these steps
The Code to write inside the Data grid Source Code is as below:
<asp: GridView ID="GridView1" runat="server" Height="376px" Style="z-index:
100; left: 8px;
position: absolute; top: 128px" Width="592px">
<Columns>
<asp: BoundField DataField="job_id" HeaderText="jobID" ReadOnly="true" />
<asp: BoundField DataField="desc" HeaderText="Description"/>
<asp: BoundField DataField="min" HeaderText="MinLvl"/>
<asp: TemplateField>
<ItemTemplate>
<asp: TextBox ID="txtmaxlvl" Text='<%#Eval ("max_lvl") %>' runat="server">
<EditItemTemplate>
<asp: DropDownList ID="ddlmaxlvl" runat="server">
<asp: ListItem>100</asp: ListItem>
<asp: ListItem>200</asp: ListItem>
<asp: ListItem>300</asp: ListItem>
</asp: DropDownList>
</EditItemTemplate>
</asp: TextBox>
</ItemTemplate>
</asp: TemplateField>
<asp: CommandField ShowEditButton="true" ShowCancelButton="true" />
</Columns>
</asp: TemplateField>
</asp: GridView>


The Code to write in the View Button Click Code is below:
protected void Button1_Click (object sender, EventArgs e)
{
  {
      BindData ();
  {
       void BindData ();
  {
         SqlConnection=new SqlConnection ("User id=sa, database="pubs);
         cn.Open ();
         Dataset ds=new Dataset ();
         SqlData Adaptor Dataset=new SqlDataAdaptor ("Select * from job", cn);
         Data. Fill (ds,"jobs");
         GridView1.DataBindSome=ds.Tables [0];
         GridView1.DataBind ();
     }
     }
     }
   }
}

-Then run and click on View Button we have to get the result else once again check the code and try.
-or type the following aspnet_regiis-I in Visual Studio Command prompt then enter we have to get a message
 like started installing and then again start run the programmed.
-Then select Grid view go to properties then enter select events and then select Row Editing then double click
  on the that and write the code as follows.
   {
       Gridview1.EditIndex=e.New EditIndex
       BindData ();
   }
Select Row Cancelling Edit then double-click and write the following code.
     {
       Gridview1 EditIndex=-1;
       Bind Data ();
     }
Note: ”e” used to indicate the particular content.
Select Row Updating and double click and write this code
 {
    Response. Write (Gridview1.Rows [e.RowIndex].Cells [0].Text;
    Textbox tjobdese, tminlvl;
    DropdownList dlmaxlvl;
    Tjobdesc= (TextBox) Gridview1.Rows [e.RowIndex}.Cells [1].Cells [1].controls [0];
    Tminlvl= (TextBox) Gridview1.Rows {e.RowIndex].Cells [2].Controls [0];
    Dlmaxlvl= (DropdownList) Gridview1.Rows [e.RowIndex].Findcontrol (“ddlmaxlvl”);
    Response. Write(s);
    Response. Write (tjobdesc.text);
    Response. Write (tminlvl.text);
    Response. Write (dlmaxlvl.Selecteditem.Tostring ());
   //We can also write code to Update db.after Updation.
    Gridview1.EditIndex=-1;


    Bind Data ();
Note: To Check the commands.
-to get the data to the dropdown list from another table.
-take Sql Data some and drop then configure data source.
-connection strings then take the tables then check column which we want
-then go to source and then remove drop down list and in DropdownList, Data source ID=”Sql Data
Source1=”Data
TextFill=”qty”.
<asp: Dropdown List Id=”ddlmaxlvl” runat=”server” Datasource Id=”Sql Data Source 1” Data Text Field=”qty”>

No comments:

Post a Comment