Javatpoint Logo
Javatpoint Logo

GridView C#

Introduction:

GridView is a powerful and versatile data presentation control in C# that allows developers to display data in a tabular format with sorting, paging, and editing capabilities. Data from a database or other information sources can be shown using this method frequently in online applications. In this article, we will explore GridView in C# in detail, including its features, properties, and how to use it to display data.

What is GridView in C#?

Data can be displayed in a tabular manner with sorting, paging, and editing options using the C# data presentation control GridView. The namespace Web.UI.WebControls is frequently used in web applications. GridView can be bound to various data sources like databases, XML, and other data sources.

Features of GridView in C#:

GridView in C# has several features that make it a popular choice for displaying data in web applications. Some of the key features of GridView are:

  • Sorting:

GridView allows the sorting of data in ascending or descending order. By pressing and holding down the shift key while clicking on the column headers, you can sort across several columns.

  • Paging:

GridView supports the paging of data, which allows you to display a subset of data at a time. This helps to increase the application's performance when dealing with large amounts of data.

  • Editing:

GridView provides editing capabilities, which allow users to edit the data directly in control. This feature is useful when the user needs to make changes to the data displayed in the control.

  • Selection:

GridView allows the selection of data rows, which can be utilized to perform various tasks on the selected rows, like deletion, updating, or exporting.

  • Formatting:

GridView provides formatting options for data displayed in the control. You can manipulate the font, size, color, and other formatting options for the data.

Properties of GridView in C#:

GridView in C# has several properties that can be used to customize its appearance and behavior. Some of the key properties of GridView are:

  • AutoGenerateColumns:

This property allows you to automatically generate columns based on the data source.

  • AllowSorting:

This property allows you to enable or disable data sorting in the GridView.

  • AllowPaging:

This property allows you to enable or disable the paging of data in the GridView.

  • ShowHeader:

This property allows you to show or hide the header of the GridView.

  • ShowFooter:

This property allows you to show or hide the footer of the GridView.

  • EditIndex:

This property allows you to set the index of the row being edited in the GridView.

Using GridView in C#:

Using GridView in C# is easy and straightforward. The first thing to do is to give your web form a GridView control. We can implement it by dragging and dropping the control from the toolbox onto your form.

Next, you need to bind the GridView control to your data source. Setting the data source as the GridView control's DataSource attribute will do this. If you wish to automatically create columns based on the data source, you can also set the AutoGenerateColumns attribute to true.

Once you have bound the GridView control to your data source, you can customize its appearance and behavior by setting various properties like AllowSorting, AllowPaging, and EditIndex.

Moreover, GridView has a number of events that you can utilize to modify its behavior. Some of the key events of GridView are:

  • RowDataBound:

This event is fired when a row is bound to the GridView. This event allows you to change how the row looks.

  • RowEditing:

This event is triggered whenever a row in the GridView is edited. This event can be used to validate data or execute other actions on the row.

  • RowDeleting:

This event is triggered whenever a row in the GridView is deleted. You can use this event to confirm the deletion or perform any other operation before deleting the row.

  • PageIndexChanged:

This event is fired when the user changes the page in the GridView. You can use this event to rebind the GridView with the new data.

Let's take a simple example of using GridView in C#. In this example, we will bind the GridView control to a dataset and display the data in a tabular format with sorting and paging capabilities.

1. First, add a GridView control to your web form.

2. Next, create a dataset and add some data to it.

C# Code:

3. Bind the GridView control to the dataset.

C# Code:

4. Enable sorting and paging in the GridView control.

C# Code:

5. Run the application and see the data displayed in the GridView control.

Conclusion:

The GridView data presentation interface in C# is strong and flexible, allowing you to display data in a tabular manner with sorting, paging, and editing options. Data from a database or other data sources are frequently displayed using it in web applications. In this article, we explored GridView in C# in detail, including its features, properties, and how to use it to display data. We also provided an example of using GridView in C# to display data from a dataset. With its rich set of features and properties, GridView in C# is an excellent choice for displaying data in web applications.


Next TopicHTML Encode C#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA