- Details
- Written by: Stanko Milosev
- Category: Windows Forms
- Hits: 1690
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.
- Details
- Written by: Stanko Milosev
- Category: Windows Forms
- Hits: 1602
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);
}
- Details
- Written by: Stanko Milosev
- Category: Windows Forms
- Hits: 2091
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
- Details
- Written by: Stanko Milosev
- Category: Windows Forms
- Hits: 2404
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