C# - How To Add And Update A DataGridView Row Using InputBoxes In C#
In This C# Tutorial We Will See:
How To Add A Row To DataGridView From InpuBoxes
How To Update A DataGridView Selected Row With InputBoxes
In C# Programming Language.
How To Add A Row To DataGridView From InpuBoxes
How To Update A DataGridView Selected Row With InputBoxes
In C# Programming Language.
Project Source Code:
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;
using Microsoft.VisualBasic;
namespace WindowsFormsApplication1
{
public partial class Csharp_Add_Update_DatagridView_Row_Using_InputBox : Form
{
public Csharp_Add_Update_DatagridView_Row_Using_InputBox()
{
InitializeComponent();
}
DataTable table = new DataTable();
int selectedRowIndex;
private void Csharp_Add_Update_DatagridView_Row_Using_InputBox_Load(object sender, EventArgs e)
{
// populate datagridview row
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));
table.Rows.Add(1, "AAA", "BBB", 32);
table.Rows.Add(2, "CCC", "DDD", 23);
table.Rows.Add(3, "EEE", "FFF", 16);
dataGridView1.DataSource = table;
}
// button add row
private void BtnAddRow_Click(object sender, EventArgs e)
{
string id = Interaction.InputBox("Enter The Id", "Enter Data", "", -1, -1);
string fn = Interaction.InputBox("Enter The First Name", "Enter Data", "", -1, -1);
string ln = Interaction.InputBox("Enter The Last Name", "Enter Data", "", -1, -1);
string age = Interaction.InputBox("Enter The Age", "Enter Data", "", -1, -1);
table.Rows.Add(int.Parse(id), fn, ln, int.Parse(age));
dataGridView1.DataSource = table;
}
// get selected row index
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
selectedRowIndex = e.RowIndex;
}
// button update selected row
private void BtnUpdateRow_Click(object sender, EventArgs e)
{
DataGridViewRow row = new DataGridViewRow();
row = dataGridView1.Rows[selectedRowIndex];
// old data
string oId = row.Cells[0].Value.ToString();
string oFn = row.Cells[1].Value.ToString();
string oLn = row.Cells[2].Value.ToString();
string oAge = row.Cells[3].Value.ToString();
string id = Interaction.InputBox("Update The Id ", "Change Data", oId, -1, -1);
string fn = Interaction.InputBox("Update The First Name ", "Change Data", oFn, -1, -1);
string ln = Interaction.InputBox("Update The Last Name ", "Change Data", oLn, -1, -1);
string age = Interaction.InputBox("Update The Age ", "Change Data", oAge, -1, -1);
row.Cells[0].Value = int.Parse(id);
row.Cells[1].Value = fn;
row.Cells[2].Value = ln;
row.Cells[3].Value = age;
}
}
}
///////////////OUTPUT:
datagridview add update row |
ALSO SEE :
Add A Row To DataGridView From TextBox
Remove A Row From DataGridView
Get Selected Row Values From DataGridView Into TextBoxUpdate A DataGridView Row With TextBox
Bagikan
C# - How To Add And Update DataGridView Row Using InputBoxes In C#
4/
5
Oleh
insurance