News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

DataGridView control

Started by jj2007, September 09, 2020, 06:14:02 PM

Previous topic - Next topic

jj2007

MSDN:
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:
Quotetmp\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

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

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):
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:
To run this example, replace the value of the connectionString variable with a connection string that is valid for your system.

TimoVJL

Just use
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

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

Quote from: TouEnMasm on September 09, 2020, 10:25:52 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

Quote from: TimoVJL on September 09, 2020, 08:47:29 PM
Just use
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:
QuoteFor 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

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

Hi Timo,

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

TimoVJL

#9
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.
May the source be with you

TouEnMasm

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