asp.net session 13 14

23
GridView Session 13-14

Upload: sisir-ghosh

Post on 27-May-2015

293 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: ASP.NET Session 13 14

GridView

Session 13-14

Page 2: ASP.NET Session 13 14

Objectives

• Introduction of DataSet• Gridview control• Several operation with GridView

Page 3: ASP.NET Session 13 14

DataSet Object

• Replaces the ADO Recordset

• Represents a cache of data that contains tables, columns, relationships, and constraints, just like a database

• Regardless of where the source data comes from, data can all be placed into DataSet objects

• Tracks changes that are made to the data it holds before updating the source data.

Page 4: ASP.NET Session 13 14

As revolutionary as a DataSet might be, it is not the best choice in every situation. Often, using a DataSet might not be appropriate; instead, using a DataReader might be better.

Because the DataSet is a disconnected copy of the data, you can work with the same records repeatedly without having to go back to the data store. This capability can greatly increase performance and lessen the load upon the server.

Page 5: ASP.NET Session 13 14

Gridview

• The GridView control is a powerful data grid control that allows you to display an entire collection of data,add sorting and paging, and perform inline editing.

• GridView by dragging the control onto the design surface of an ASP.NET Web page.

• In column definition TemplateField &ItemTemplate we will insert any control .

Page 6: ASP.NET Session 13 14
Page 7: ASP.NET Session 13 14

• Customizing Columns in the GridView:

If you have your grid configured to automatically generate columns based on the bound data source,

the grid creates fields for each public property exposed by the data source.

Page 8: ASP.NET Session 13 14
Page 9: ASP.NET Session 13 14

Field Control DescriptionBoundField Displays the value of a field in a data source. This is

the default column type of the GridView control.

CheckBoxField Displays a check box for each item in the GridView control. This column field type is commonly used to display fields with a Boolean value.

HyperLinkField Displays the value of a field in a data source as a hyperlink. This column field type allows you to bind a second field to the hyperlink’s URL.

ButtonField Displays a command button for each item in the GridView control. This allows you to create a column of custom button controls, such as an Add or Remove button.

ImageField Automatically displays an image when the data in the field represents an image.

Page 10: ASP.NET Session 13 14

Edit,Update,Delete Operation in GridView

Page 11: ASP.NET Session 13 14

Event Generate Code• GridView_course.RowDeleting += new

GridViewDeleteEventHandler(GridView_course_RowDeleting);

• GridView_course.RowEditing += new GridViewEditEventHandler(GridView_course_RowEditing);

• GridView_course.RowUpdating += new GridViewUpdateEventHandler(GridView_course_RowUpdating);

• GridView_course.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView_course_RowCancelingEdit);

Page 12: ASP.NET Session 13 14

RowCancelingEdit Event Execution Code

• void GridView_course_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

• {• GridView_course.EditIndex = -1;• DataSet ds_Row = new DataSet();• ds_Row = dcon.Data_inventer("Select Subject

as Subject,courseOffer as Courses_Offer from Course order by Subject");

• GridView_course.DataSource = ds_Row;• GridView_course.DataBind();• }

Page 13: ASP.NET Session 13 14

RowEditing Event Execution Code

• void GridView_course_RowEditing(object sender, GridViewEditEventArgs e)

• {• GridView_course.EditIndex = e.NewEditIndex;• DataSet ds_Row = new DataSet();• ds_Row = dcon.Data_inventer("Select Subject as

Subject,courseOffer as Courses_Offer from Course order by Subject");

• GridView_course.DataSource = ds_Row;• GridView_course.DataBind();• • }

Page 14: ASP.NET Session 13 14

RowDeleting Event Execution Code

• void GridView_course_RowDeleting(object sender, GridViewDeleteEventArgs e)

• {• dcon.inupdl("delete from Course where Subject='" +

GridView_course.Rows[e.RowIndex].Cells[3].Text + "'");• DataSet ds_Row = new DataSet();• ds_Row = dcon.Data_inventer("Select Subject as

Subject,courseOffer as Courses_Offer from Course order by Subject");

• GridView_course.DataSource = ds_Row;• GridView_course.DataBind();• }

Page 15: ASP.NET Session 13 14

RowUpdating Event Execution Code

• void GridView_course_RowUpdating(object sender, GridViewUpdateEventArgs e)

• {• TextBox T_Subject =

(TextBox)GridView_course.Rows[e.RowIndex].Cells[3].Controls[0];

• TextBox T_CourseOff = (TextBox)GridView_course.Rows[e.RowIndex].Cells[4].Controls[0];

• dcon.inupdl("Update Course set courseOffer='" + T_CourseOff.Text + "' Where Subject='" + T_Subject.Text + "'");

Page 16: ASP.NET Session 13 14

• DataSet ds_Row = new DataSet();• ds_Row = dcon.Data_inventer("Select

Subject as Subject,courseOffer as Courses_Offer from Course order by Subject");

• GridView_course.DataSource = ds_Row;

• GridView_course.EditIndex = -1;• GridView_course.DataBind();• }

Page 17: ASP.NET Session 13 14

“FirstSample” is the key name in web config. Now Connection open & close• public SqlConnection open()• {• con_course.Open();• return con_course;• }• public SqlConnection close()• {• con_course.Close();• return null;• }

Page 18: ASP.NET Session 13 14

Simple code analysis

Creating a connection class: Using system.Data.Sqlclient; SqlConnection con_course;• //Connection Establishment With siliguri DataBase• public SqlConnection Siliguri_conn()• {• con_course = new

SqlConnection(ConfigurationManager.AppSettings["FirstSample"]);

• return con_course;• }

Page 19: ASP.NET Session 13 14

Insert Update Delete Command for DataBase

• public void inupdl(string str)• {• SqlCommand comm = new SqlCommand();• comm.Connection = Siliguri_conn();• comm.CommandText = str;• open();• comm.ExecuteNonQuery();• close();• }

Page 20: ASP.NET Session 13 14

Fill Datatable into DataSet From DataBase

• public DataSet Data_inventer(string str) { SqlDataAdapter adpt_inven = new

SqlDataAdapter(str, Siliguri_conn());• DataSet ds = new DataSet();• adpt_inven.Fill(ds);• return ds;• }

Page 21: ASP.NET Session 13 14

Execute statement

Default.aspx.cs page: Create a object for connec class connec com_info = new connec();Now run insert,Update & Delete command com_info.inupdl("insert into TestValue values('" +

TextBox1.Text + "','" + TextBox2.Text + "')");Now retrieve data from Table: DataSet ds=new DataSet(); ds. Data_inventer(“Select * From TestValue);

Page 22: ASP.NET Session 13 14

Binding Data into Table

GridView1.DataSource = ds; GridView1.DataBind();

Page 23: ASP.NET Session 13 14

Summery

• GridView Controls.• Several Operation into Gridview Control.• Connection with SQL.Server program and

execute SQL command.