milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. C#

Read dataset's XML and display all contained tables

Details
Written by: Stanko Milosev
Category: Windows Forms
Published: 12 January 2022
Last Updated: 12 January 2022
Hits: 605
I have saved an XML with DataSet.WriteXml, then I wanted to display values of that XML in a DataGridView, where each DataTable contained in that XML will create new DataGridView. Here I already gave one example of DataTables. Here is code:
DataSet ds = new DataSet();
ds.ReadXml(@"test.xml");

foreach (DataTable dataTable in ds.Tables)
{
	DataGridView dataGridView = new DataGridView();
	dataGridView.Dock = DockStyle.Top;
	dataGridView.DataSource = dataTable;

	dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
	dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
	dataGridView.AllowUserToOrderColumns = true;
	dataGridView.AllowUserToResizeColumns = true;

	Controls.Add(dataGridView);

	Label label = new Label();
	label.Text = dataTable.TableName;
	label.Dock = DockStyle.Top;
	Controls.Add(label);
}
Example download from here without XML attached.

Read dataset's XML and display all contained tables

Details
Written by: Stanko Milosev
Category: Windows Forms
Published: 12 January 2022
Last Updated: 12 January 2022
Hits: 535
I have saved an XML with DataSet.WriteXml, then I wanted to display values of that XML in a DataGridView, where each DataTable contained in that XML will create new DataGridView:
DataSet ds = new DataSet();
ds.ReadXml(@"test.xml");

foreach (DataTable dataTable in ds.Tables)
{
	DataGridView dataGridView = new DataGridView();
	dataGridView.Dock = DockStyle.Top;
	dataGridView.DataSource = dataTable;

	dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
	dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
	dataGridView.AllowUserToOrderColumns = true;
	dataGridView.AllowUserToResizeColumns = true;

	Controls.Add(dataGridView);

	Label label = new Label();
	label.Text = dataTable.TableName;
	label.Dock = DockStyle.Top;
	Controls.Add(label);
}

Grid, DataSet, BindingSource and TableAdapter another example

Details
Written by: Stanko Milosev
Category: Windows Forms
Published: 08 May 2021
Last Updated: 08 May 2021
Hits: 864
Another example of DataGridView this time with run-time DataSource binding:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace GridDataSetBindingSourceAndTableAdapterDynamicExample
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      string connectionString =
        "data source=localhost;initial catalog=TestCompleteTest;persist security info=True;Integrated Security=SSPI";

      using (SqlConnection conn = new SqlConnection(connectionString))
      {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Names", conn))
        {
          conn.Open();
          DataSet ds = new DataSet();
          SqlDataAdapter da = new SqlDataAdapter
          {
            SelectCommand = cmd
          };
          da.Fill(ds);
          dataGridView1.DataSource = ds.Tables[0];
        }
      }
    }
  }
}

Taken from here

Example download from here

Grid, DataSet, BindingSource and TableAdapter example

Details
Written by: Stanko Milosev
Category: Windows Forms
Published: 25 April 2021
Last Updated: 25 April 2021
Hits: 956
Here is my example of using DataGridView in Visual Studio 2019.

First I started new Windows Forms App (.NET Framework), please note that you need .NET Framework, otherwise it will .NET 5.0

Next I added DataGridView, and here automatically opened "Choose Data Source":

Then choose a data source type, in my case I have choosen Database:

Choose data connection:

Choose database objects:

I have added two buttons, one to update data to the database, and other to fill grid with data. On the end my Form looks like:

Finally, here is how my source code look like:

using System;
using System.Windows.Forms;

namespace GridDataSetBindingSourceAndTableAdapterExample
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      // TODO: This line of code loads data into the 'testCompleteTestDataSet.Names' table. You can move, or remove it, as needed.
      this.namesTableAdapter.Fill(this.testCompleteTestDataSet.Names);

    }

    private void Update_Click(object sender, EventArgs e)
    {
      this.namesTableAdapter.Update(this.testCompleteTestDataSet);
    }

    private void Fill_Click(object sender, EventArgs e)
    {
      this.namesTableAdapter.Fill(this.testCompleteTestDataSet.Names);
    }
  }
}
Here is SQL script to create table which I am using for this example:
CREATE TABLE [dbo].[Names](
	[FirstName] [nvarchar](50) NULL,
	[LastName] [nvarchar](50) NULL,
	[ID] [int] NULL
) ON [PRIMARY]
Example project in VS 2019 download from here
  1. Add items to UltraGrid
  2. Image opacity
  3. Inheriting Windows Forms with Visual C# .NET

Subcategories

WPF

Beginning

Code snippets

NUnit

LINQ

Windows Forms

Page 32 of 33

  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33