0% found this document useful (0 votes)
26 views

Int Selectedrow : // Add Columns To Datatable

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Int Selectedrow : // Add Columns To Datatable

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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
table.Columns.Add("Id", typeof(int));
table.Columns.Add("First Name", typeof(string));
table.Columns.Add("Last Name", typeof(string));
table.Columns.Add("Age", typeof(int));

// add rows to datatable


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

dataGridView1.DataSource = table;
}

// button add row to datagridview


private void btnAdd_Click(object sender, EventArgs e)
{
// add row to datatable from textboxes
table.Rows.Add(textBoxID.Text, textBoxFN.Text, textBoxLN.Text,
textBoxAGE.Text);
dataGridView1.DataSource = table;

// datagridview cell click


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

// display datagridview selected row data into textboxes


textBoxID.Text = row.Cells[0].Value.ToString();
textBoxFN.Text = row.Cells[1].Value.ToString();
textBoxLN.Text = row.Cells[2].Value.ToString();
textBoxAGE.Text = row.Cells[3].Value.ToString();
}

// button update datagridview selected row


private void btnUpdate_Click(object sender, EventArgs e)
{
DataGridViewRow newDataRow = dataGridView1.Rows[selectedRow];
newDataRow.Cells[0].Value = textBoxID.Text;
newDataRow.Cells[1].Value = textBoxFN.Text;
newDataRow.Cells[2].Value = textBoxLN.Text;
newDataRow.Cells[3].Value = textBoxAGE.Text;

// button remove
private void Delete_Click(object sender, EventArgs e)
{
// delete datagridview row selected row
selectedRow = dataGridView1.CurrentCell.RowIndex;
dataGridView1.Rows.RemoveAt(selectedRow);

}
}
}

You might also like