Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Load data grid in C #. Example here in WinForms, VSTO add-in

24.10.2018 (👁211)


 

How to fill a data grid in Windows at runtime in C #

 

Shown is a Word Add-in, which should display data from Excel and allow a selection.

In a Word addin as Ribbonbar, additional elements are usually added as a Windows Forms element.

A DataGrid is also initially a DataGridView. Filling a data grid is similar in Winforms and WPF.

 

Subject:

VSTO Addin, Windows Forms Form, WinForm, DataGridView, DataGridViewRow

Dynamic loading of a DataGridView at runtime. Programmatically loading a grid

 

Data grid loaded with C #.

Here in Word Addin. Clicking on a Ribbonbar button opens a Form-Dialog window with a data table.

 

1

2

3

4

5

 

11

22

33

44

55

 

111

222

333

444

555

 

Design view of the form

The form in VSTO Addin is loaded as a Windows Forms form.

 

The data grid is first dragged into the form via the Design Editor.

 

Fill data grid

DataGridView

 

Setting the DataGridView

Add columns

The first step is to add the columns at runtime

            dataGrid_Excel.Columns.Add("column_01""A");

            dataGrid_Excel.Columns.Add("column_02""B");

            dataGrid_Excel.Columns.Add("column_03""C");

            dataGrid_Excel.Columns.Add("column_04""D");

            dataGrid_Excel.Columns.Add("column_05""E");

 

The individual data series can be added directly as text in Example 1

dataGrid_Excel.Rows.Add("111","222""333""444""555");

 

When working with dynamic data, you can pass the parameters as variables

dataGrid_Excel.Rows.Add(cell_Values[0],cell_Values[1], cell_Values[2], cell_Values[3], cell_Values[4]);

 

frmExcel_Data.cs

Programming the DataGridView

 

using System;

 

using System.Windows.Forms;

using Excel = Microsoft.Office.Interop.Excel;

 

namespace addin_Excel_01

{

    public partial class frmExcel_Data : Form

    {

        public frmExcel_Data()

        {

            InitializeComponent();

        }

 

        private void frmExcel_Data_Load(object sender, EventArgs e)

        {

            //-----------------< load_Excel() >-----------------

            dataGrid_Excel.AllowUserToAddRows = false;

 

            //--< define Columns >--

            dataGrid_Excel.Columns.Add("column_01""A");

            dataGrid_Excel.Columns.Add("column_02""B");

            dataGrid_Excel.Columns.Add("column_03""C");

            dataGrid_Excel.Columns.Add("column_04""D");

            dataGrid_Excel.Columns.Add("column_05""E");

            //--</ define Columns >--

 

 

            //----< DataRow1 >----

            //--< get Values >--

            String[] cell_Values = new String[5];

            cell_Values[0] = "1";

            cell_Values[1] = "2";

            cell_Values[2] = "3";

            cell_Values[3] = "4";

            cell_Values[4] = "5";

            //--</ get Values >--

 

 

            //-< add_Data_Row >-

            //< add >

            int iRow = dataGrid_Excel.Rows.Add(new DataGridViewRow());

            DataGridViewRow row = dataGrid_Excel.Rows[iRow];

            //</ add >

            //< fill >

            row.Cells[0].Value = cell_Values[0];

            row.Cells[1].Value = cell_Values[1];

            row.Cells[2].Value = cell_Values[2];

            row.Cells[3].Value = cell_Values[3];

            row.Cells[4].Value = cell_Values[4];

            //</ fill >

            //-</ add_Data_Row >-

            //----</ DataRow1 >----

 

 

            //----< DataRow2 >----

            //--< get Values >--

            cell_Values = new String[5];

            cell_Values[0] = "11";

            cell_Values[1] = "22";

            cell_Values[2] = "33";

            cell_Values[3] = "44";

            cell_Values[4] = "55";

            //--</ get Values >--

 

            //-< add >-

            dataGrid_Excel.Rows.Add(cell_Values[0],cell_Values[1], cell_Values[2], cell_Values[3], cell_Values[4]);

            //-</ add >-

            //----</ DataRow2 >----

 

 

            //----< DataRow3 >----

            //-< add >-

            dataGrid_Excel.Rows.Add("111","222""333""444""555");

            //-</ add >-

            //----</ DataRow3 >----

 

 

            //-----------------< load_Excel() >-----------------

        }

    }

}

 

 

Call form

The form is called with the form name under the VSTO Addin project

frmExcel_Data frm = new frmExcel_Data();

frm.Show();

 

In the Ribbonbar, the Windows Forms form is called by .Show () and can then work like a fully-fledged Windows Forms form with all Windows functions

Winforms