Author Topic: DataGridView control  (Read 1012 times)

jj2007

  • Member
  • *****
  • Posts: 11145
  • Assembler is fun ;-)
    • MasmBasic
DataGridView control
« on: September 09, 2020, 06:14:02 PM »
MSDN:
Code: [Select]
using System;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Windows.Forms;

namespace WindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}
public class Form1 : Form
{
    private DataGridView dataGridView1 = new DataGridView();
    private BindingSource bindingSource1 = new BindingSource();
    private SqlDataAdapter dataAdapter = new SqlDataAdapter();
    private Button reloadButton = new Button();
    private Button submitButton = new Button();

    [STAThread()]
    public static void Main()
    {
        Application.Run(new Form1());
    }

    // Initialize the form.
    public Form1()
    {
        dataGridView1.Dock = DockStyle.Fill;

        reloadButton.Text = "Reload";
        submitButton.Text = "Submit";
        reloadButton.Click += new EventHandler(ReloadButton_Click);
        submitButton.Click += new EventHandler(SubmitButton_Click);

        FlowLayoutPanel panel = new FlowLayoutPanel
        {
            Dock = DockStyle.Top,
            AutoSize = true
        };
        panel.Controls.AddRange(new Control[] { reloadButton, submitButton });

        Controls.AddRange(new Control[] { dataGridView1, panel });
        Load += new EventHandler(Form1_Load);
        Text = "DataGridView data binding and updating demo";
    }

    private void GetData(string selectCommand)
    {
        try
        {
            // Specify a connection string.
            // Replace <SQL Server> with the SQL Server for your Northwind sample database.
            // Replace "Integrated Security=True" with user login information if necessary.
            String connectionString =
                "Data Source=<SQL Server>;Initial Catalog=Northwind;" +
                "Integrated Security=True";

            // Create a new data adapter based on the specified query.
            dataAdapter = new SqlDataAdapter(selectCommand, connectionString);

            // Create a command builder to generate SQL update, insert, and
            // delete commands based on selectCommand.
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

            // Populate a new data table and bind it to the BindingSource.
            DataTable table = new DataTable
            {
                Locale = CultureInfo.InvariantCulture
            };
            dataAdapter.Fill(table);
            bindingSource1.DataSource = table;

            // Resize the DataGridView columns to fit the newly loaded content.
            dataGridView1.AutoResizeColumns(
                DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
        }
        catch (SqlException)
        {
            MessageBox.Show("To run this example, replace the value of the " +
                "connectionString variable with a connection string that is " +
                "valid for your system.");
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Bind the DataGridView to the BindingSource
        // and load the data from the database.
        dataGridView1.DataSource = bindingSource1;
        GetData("select * from Customers");
    }

    private void ReloadButton_Click(object sender, EventArgs e)
    {
        // Reload the data from the database.
        GetData(dataAdapter.SelectCommand.CommandText);
    }

    private void SubmitButton_Click(object sender, EventArgs e)
    {
        // Update the database with changes.
        dataAdapter.Update((DataTable)bindingSource1.DataSource);
    }
}

As usual, every snippet that comes from a language with a "C" in it's name but is not C itself throws cryptic errors:
Quote
tmp\TmpC#.cs(13,13): error CS0103: Il nome 'InitializeComponent' non esiste nel contesto corrente.
tmp\TmpC#.cs(42,9): error CS1526: Un'espressione new richiede () o [] dopo il tipo.
tmp\TmpC#.cs(73,13): error CS1526: Un'espressione new richiede () o [] dopo il tipo

My intention was to let Olly tell me how to "bind" data to the control. Anybody able to make it compile...?

TimoVJL

  • Member
  • ****
  • Posts: 669
Re: DataGridView control
« Reply #1 on: September 09, 2020, 06:48:45 PM »
It's C# C-sharp not C, so you need a different debugger for it.

Comment out that InitializeComponent(); and you can compile and run it.
May the source be with you

jj2007

  • Member
  • *****
  • Posts: 11145
  • Assembler is fun ;-)
    • MasmBasic
Re: DataGridView control
« Reply #2 on: September 09, 2020, 06:57:15 PM »
Timo,
This compiles perfectly, so I assume I have the right C# 'debugger' (and no, commenting out the InitializeComponent() doesn't solve the problem, the other two errors are still there):
Code: [Select]
using System;
// c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe HelloC#.cs
class HelloCS
{
static void Main()
{
Console.WriteLine("Hello World, how are you today?");
string line;
line = Console.ReadLine();
}
}

Now I managed to compile it, with a little help from my friends at Google and CodeProject, but the exe complains:
Code: [Select]
To run this example, replace the value of the connectionString variable with a connection string that is valid for your system.

TimoVJL

  • Member
  • ****
  • Posts: 669
Re: DataGridView control
« Reply #3 on: September 09, 2020, 08:47:29 PM »
Just use
Code: [Select]
C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
That connection string expect you to have that database installed.
May the source be with you

TouEnMasm

  • Member
  • *****
  • Posts: 1577
    • EditMasm
Re: DataGridView control
« Reply #4 on: September 09, 2020, 10:25:52 PM »
Hello,
Why use c to connect a database ?
asm can do that.
https://codes-sources.commentcamarche.net/source/15287-lecture-base-de-donnees-excel-et-access-avec-masm32
It is old,so,If it don't work at the first time,it's not my fault,see microsoft( :mrgreen:).
But you have good chances to make it work.
Fa is a musical note to play with CL

jj2007

  • Member
  • *****
  • Posts: 11145
  • Assembler is fun ;-)
    • MasmBasic
Re: DataGridView control
« Reply #5 on: September 09, 2020, 10:53:50 PM »
asm can do that.

Yes indeed, and it's very simple!

GuiParas equ "MasmBasic: fast and easy to use", w600, h700, bGrey, icon Plot   ; width 600px, grey background
include \masm32\MasmBasic\Res\MbGui.asm
GuiControl MyEdit, "edit"      ; simplest case, creates an edit control that uses the entire main window
  xlsConnect                            ; no args=Excel, System
  .if !Zero?                           ; non-zero means success
       xlsOpen "\Masm32\MasmBasic\Res\LifeExOecd.xls"  ; we open a file
       .if !Zero?                      ; non-zero signals success
                xlsConnect "LE Males at birth"  ; tell Excel to which sheet you want to talk
                .if !Zero?              ; non-zero signals success
                                AddWin$ hMyEdit="Current selection=["+xlsRead$()+"]"+CrLf$      ; no args means current selection
                .endif
                xlsClose 0              ; close the file without saving (0=don't save, 1=save, no arg: ask)
        .endif
        xlsDisconnect                   ; say bye to Excel
  .endif
GuiEnd

jj2007

  • Member
  • *****
  • Posts: 11145
  • Assembler is fun ;-)
    • MasmBasic
Re: DataGridView control
« Reply #6 on: September 09, 2020, 10:59:14 PM »
Just use
Code: [Select]
C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
That connection string expect you to have that database installed.

Yes, but that's a runtime problem. I had trouble with the compiler - and I am not the only one, see on SOF The name 'InitializeComponent' does not exist in the current context:
Quote
For those who have no errors in Debug mode, but do have the specified error in Release mode (and yet the project runs fine), here is something simple to try:

Open the XAML file corresponding to the offending xaml.cs file.
Make an edit--any edit, like add a space somewhere
Save the file and close it
This method worked for me in VS 2015, and according to other users, also 2017 and 2019

Would you have thought that is was so simple to fix this error? :greenclp:

TimoVJL

  • Member
  • ****
  • Posts: 669
Re: DataGridView control
« Reply #7 on: September 09, 2020, 11:05:59 PM »
ODBC is good for databases.
I have some tiny programs using oracle and using ODBC DriverConnect.
A minimal dll-set is enough for it, no oracle installations.
The oracle is the worst ODBC driver system ever build ?

May the source be with you

Vortex

  • Member
  • *****
  • Posts: 2508
Re: DataGridView control
« Reply #8 on: September 10, 2020, 12:19:31 AM »
Hi Timo,

If I am not wrong, you need additionaly the Oracle client application to access the Oracle database.

TimoVJL

  • Member
  • ****
  • Posts: 669
Re: DataGridView control
« Reply #9 on: September 10, 2020, 04:22:44 AM »
My small apps works from a shared folder, so no need of installations.
Just do the same :biggrin:
A wink, i use sqora32.dll directly as a ODBC client.

SQLRelay is a one other option for programs to avoid a big client library.
« Last Edit: September 10, 2020, 07:27:14 PM by TimoVJL »
May the source be with you

Alexd010

  • Regular Member
  • *
  • Posts: 1
Re: DataGridView control
« Reply #10 on: December 21, 2020, 10:15:57 PM »
Hello,
Why use c to connect a database ?
asm can do that.
taxi orly
It is old,so,If it don't work at the first time,it's not my fault,see microsoft( :mrgreen:).
But you have good chances to make it work.
I am a newbie to this but so far the only post processor and I have not had these grooves you show.

TouEnMasm

  • Member
  • *****
  • Posts: 1577
    • EditMasm
Re: DataGridView control
« Reply #11 on: December 22, 2020, 12:55:37 AM »
Quote
I am a newbie to this but so far the only post processor and I have not had these grooves you show.
If this mean you haven't problem to compile it,it is good.
For me me who have made the source,i have change the includes files and this have needed a little work.
Fa is a musical note to play with CL