Affichage des articles dont le libellé est Add Row To DataGridView. Afficher tous les articles
Affichage des articles dont le libellé est Add Row To DataGridView. Afficher tous les articles

Add Row To Datagridview From Another Form In C#

How to Add a New Row To Datagridview From Another Form Using C#

Add Row To Datagridview From Another Form Using C#


In This C# Tutorial We Will See How To Insert A New Row To a Datagridview With Data From TextBoxes In Another Form Using C# Windows Form and Visual Studio Editor .

WATCH THIS C# TUTORIAL


Project Source Code:


// ------ First Create a Form  Where You Put The Datagridview 

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 Csharp_Tutorials.rst
{
    public partial class FRM_GRIDVIEW : Form
    {
        public FRM_GRIDVIEW()
        {
            InitializeComponent();
        }

// button to open the form where the textboxes are in
        private void button_NewForm_Click(object sender, EventArgs e)
        {
            FormINFO finfo = new FormINFO(this);
            finfo.Show();
        }
    }
}


Datagridview Form



// ------ Create a Form With TextBoxes

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 Csharp_Tutorials.rst
{
    public partial class FormINFO : Form
    {
        FRM_GRIDVIEW fgrid;
        public FormINFO(FRM_GRIDVIEW fg)
        {
            InitializeComponent();
            this.fgrid = fg;
        }

// button to add the row with the data to the datagridview
        private void button_Add_Click(object sender, EventArgs e)
        {
            fgrid.dataGridView1.Rows.Add(textBox1.Text, textBox2.Text, textBox3.Text);
        }

    }
}


TextBoxes Form



 - Watch The Video Tutorial To See The Scond Method ( making the datagrdivew static ) 



OUTPUT:


C# - Insert Row To Datagridview From Another Form





C# - How To Add And Update DataGridView Row Using InputBoxes In C#

c# add update row

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 UpdateDataGridView 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
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 TextBox
Update A DataGridView Row With TextBox



VB.NET - How To Add Delete And Update DataGridView Row Using TextBoxes In VBNET

VB.NET - How To Add Delete And Update Row From DataGridView Using TextBoxes In VB.NET 

                                                                                                                         

In This VB.NET Tutorial We Will See How To Add A Row To DataGridView From TextBox, Update A Selected Row Using TextBox, And Delete The Selected Row In VB.NET Programming Language.


Project Source Code:

Imports System.Data.DataTable

Public Class Add_Update_Delete_DataGridView_Row

 ' Create a new datatable
    Dim table As New DataTable("Table")

    Dim index As Integer

    Private Sub Add_Update_Delete_DataGridView_Row_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ' Add columns to your datatable, 
    ' with the name of the columns and their type 
        table.Columns.Add("Id", Type.GetType("System.Int32"))
        table.Columns.Add("First Name", Type.GetType("System.String"))
        table.Columns.Add("Last Name", Type.GetType("System.String"))
        table.Columns.Add("Age", Type.GetType("System.Int32"))

         ' Add rows to the datatable with some data
        table.Rows.Add(1, "XXXX", "YYYYY", 21)
        table.Rows.Add(2, "SSDD", "hGSQ", 33)
        table.Rows.Add(3, "fgfgd", "jgfdd", 53)
        table.Rows.Add(4, "cvfghyghj", "sdrgtyh", 19)
        table.Rows.Add(5, "hghfd", "ghjgdf", 36)
        table.Rows.Add(6, "cvvdfgh", "juyrfdvc", 63)
        table.Rows.Add(7, "aefht", "cvfhytrff", 21)
        table.Rows.Add(8, "wghyuj", "mihgdwrh", 33)
        table.Rows.Add(9, "qsztii", "bvdhjh", 53)
        table.Rows.Add(10, "rytyufd", "esdfzr", 19)

   'set data from datatable to datagridview
        DataGridView1.DataSource = table

    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

        table.Rows.Add(TextBoxID.Text, TextBoxFN.Text, TextBoxLN.Text, TextBoxAGE.Text)

        DataGridView1.DataSource = table

    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        ' get the index of the selected datagridview row
        index = e.RowIndex
        Dim selectedRow As DataGridViewRow

   ' show data from the selected row to textboxes
        selectedRow = DataGridView1.Rows(index)
        TextBoxID.Text = selectedRow.Cells(0).Value.ToString()
        TextBoxFN.Text = selectedRow.Cells(1).Value.ToString()
        TextBoxLN.Text = selectedRow.Cells(2).Value.ToString()
        TextBoxAGE.Text = selectedRow.Cells(3).Value.ToString()

    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
         ' the new row
        Dim newDataRow As DataGridViewRow

        newDataRow = DataGridView1.Rows(index)

   ' get data from textboxes to the row
        newDataRow.Cells(0).Value = TextBoxID.Text
        newDataRow.Cells(1).Value = TextBoxFN.Text
        newDataRow.Cells(2).Value = TextBoxLN.Text
        newDataRow.Cells(3).Value = TextBoxAGE.Text

    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click

        DataGridView1.Rows.RemoveAt(index)

    End Sub
End Class

///////////////OUTPUT:




vb.net add a row to datagridview using textboxes
vb.net add a row to datagridview
vb.net update a datagridview row using textboxes
vb.net update a datagridview row

vb.net delete a datagridview selected row
vb.net delete a datagridview row