Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tahir AnsariRank #366 · 6.8K reputationPosted Aug 2, 2023, 6:43 AM EDT
Hi Atish try below sample code
Step 1: Set up the GridView control in your web form:
Step 2: Bind data to the GridView:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Replace this with your data source and binding logic DataTable dataSource = GetDataFromYourSource(); GridView1.DataSource = dataSource; GridView1.DataBind(); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // Access the data and customize the row, if needed } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "SomeCommand") { // Handle the command here } } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { // Access the selected row's data here }Mohammad HussainRank #513 · 4.8K reputationPosted Aug 1, 2023, 11:36 PM EDT
using System; using System.Windows.Forms; namespace WindowsFormsApp { public partial class Form1 : Form { public Form1() { InitializeComponent(); InitializeGridView(); } private void InitializeGridView() { // Add some columns to the GridView control gridView.Columns.Add("Column1", "Column 1"); gridView.Columns.Add("Column2", "Column 2"); gridView.Columns.Add("Column3", "Column 3"); // Add some sample data to the GridView gridView.Rows.Add("Data 1", "Data 2", "Data 3"); gridView.Rows.Add("Data 4", "Data 5", "Data 6"); // Subscribe to GridView events gridView.CellClick += GridView_CellClick; gridView.CellDoubleClick += GridView_CellDoubleClick; gridView.CellValueChanged += GridView_CellValueChanged; gridView.SelectionChanged += GridView_SelectionChanged; } private void GridView_CellClick(object sender, DataGridViewCellEventArgs e) { // Handle CellClick event int rowIndex = e.RowIndex; int columnIndex = e.ColumnIndex; MessageBox.Show($"Cell Clicked: Row {rowIndex}, Column {columnIndex}"); } private void GridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { // Handle CellDoubleClick event int rowIndex = e.RowIndex; int columnIndex = e.ColumnIndex; MessageBox.Show($"Cell Double-Clicked: Row {rowIndex}, Column {columnIndex}"); } private void GridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) { // Handle CellValueChanged event int rowIndex = e.RowIndex; int columnIndex = e.ColumnIndex; DataGridViewCell cell = gridView.Rows[rowIndex].Cells[columnIndex]; string newValue = cell.Value.ToString(); MessageBox.Show($"Cell Value Changed: Row {rowIndex}, Column {columnIndex}, New Value: {newValue}"); } private void GridView_SelectionChanged(object sender, EventArgs e) { // Handle SelectionChanged event int selectedRowCount = gridView.SelectedRows.Count; MessageBox.Show($"Selection Changed: {selectedRowCount} rows selected."); } } }Cr BhargaviRank #420 · 5.9K reputationPosted Aug 3, 2023, 1:20 AM EDT
The `GridView` control in WinForms provides a tabular view of data, allowing users to display, edit, and interact with data in a grid format. Below is an overview of some of the common events available in the `GridView` control along with their purposes:
1. `CellClick`: This event occurs when the user clicks on a cell in the `GridView`. It is commonly used to handle cell-specific actions or to capture the selected cell's value.
2. `CellDoubleClick`: This event is triggered when the user double-clicks on a cell. It allows you to implement special behavior when the user rapidly interacts with a cell.
3. `RowEnter`: This event is raised when the user navigates to a new row in the `GridView`. It can be used to perform actions when a specific row is selected.
4. `RowLeave`: This event is raised when the focus leaves a row in the `GridView`. It can be useful for performing actions when the user moves to a different row.
5. `RowHeaderMouseClick`: This event occurs when the user clicks on the row header, the area on the left side of the `GridView`. It can be used to perform actions based on row selection from the header.
6. `RowHeaderMouseDoubleClick`: This event is triggered when the user double-clicks on the row header. It allows you to implement special behavior when the user rapidly interacts with the row header.
7. `CellValueChanged`: This event is raised when the value of a cell is changed by the user. It can be used to handle data changes in the `GridView`.
8. `CellFormatting`: This event allows you to customize the appearance of cells based on certain conditions. You can change the cell's font, color, or other visual attributes dynamically.
9. `CellValidating`: This event occurs when a cell's value is being validated, typically when the user moves to another cell or tries to commit the edit. It is commonly used to perform data validation before updating the underlying data source.
10. `KeyDown`: This event is triggered when a key is pressed while the `GridView` has focus. It can be used to implement keyboard shortcuts or handle specific key actions.
11. `MouseHover`: This event occurs when the mouse pointer hovers over a cell in the `GridView`. It can be used to provide tooltips or other information related to the cell.
These are just some of the common events available in the `GridView` control. Depending on your specific use case, you may use additional events and customize their behavior to meet your application's requirements. Handling these events effectively can enhance the interactivity and functionality of your `GridView` control in the WinForms application.