0% found this document useful (0 votes)
10 views2 pages

Manage DataGridView Rows in C#

This document contains code for a Windows Forms application that allows a user to add, update, and delete rows from a DataGridView control bound to a DataTable. It initializes the DataTable with sample data and sets the DataGridView's data source. Buttons are used to add new rows from textboxes, update the selected row's values, and delete the selected row. Cell click and row selection is used to populate the textboxes with the selected row's data.

Uploaded by

Wahyu Kurniawan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Manage DataGridView Rows in C#

This document contains code for a Windows Forms application that allows a user to add, update, and delete rows from a DataGridView control bound to a DataTable. It initializes the DataTable with sample data and sets the DataGridView's data source. Buttons are used to add new rows from textboxes, update the selected row's values, and delete the selected row. Cell click and row selection is used to populate the textboxes with the selected row's data.

Uploaded by

Wahyu Kurniawan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

using System;

using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace WindowsFormsApplication1
{
public partial class DataGridView_Add_Update_Delete_Selected_Row : Form
{
public DataGridView_Add_Update_Delete_Selected_Row()
{
InitializeComponent();
}

DataTable table = new DataTable();


int selectedRow;
private void DataGridView_Add_Update_Delete_Selected_Row_Load(object sender,
EventArgs e)
{
// add columns to datatable
[Link]("Id", typeof(int));
[Link]("First Name", typeof(string));
[Link]("Last Name", typeof(string));
[Link]("Age", typeof(int));

// add rows to datatable


[Link](1, "First A", "Last A", 10);
[Link](2, "First B", "Last B", 20);
[Link](3, "First C", "Last C", 30);
[Link](4, "First D", "Last D", 40);
[Link](5, "First E", "Last E", 50);
[Link](6, "First F", "Last F", 60);
[Link](7, "First G", "Last G", 70);
[Link](8, "First H", "Last H", 80);

[Link] = table;
}

// button add row to datagridview


private void btnAdd_Click(object sender, EventArgs e)
{
// add row to datatable from textboxes
[Link]([Link], [Link], [Link],
[Link]);
[Link] = table;

// datagridview cell click


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs
e)
{
// get datagridview selected row
selectedRow = [Link];
DataGridViewRow row = [Link][selectedRow];

// display datagridview selected row data into textboxes


[Link] = [Link][0].[Link]();
[Link] = [Link][1].[Link]();
[Link] = [Link][2].[Link]();
[Link] = [Link][3].[Link]();
}

// button update datagridview selected row


private void btnUpdate_Click(object sender, EventArgs e)
{
DataGridViewRow newDataRow = [Link][selectedRow];
[Link][0].Value = [Link];
[Link][1].Value = [Link];
[Link][2].Value = [Link];
[Link][3].Value = [Link];

// button remove
private void Delete_Click(object sender, EventArgs e)
{
// delete datagridview row selected row
selectedRow = [Link];
[Link](selectedRow);

}
}
}

You might also like