milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • 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#
  4. Windows Forms

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: 1676
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: 1999
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

Add items to UltraGrid

Details
Written by: Stanko Milosev
Category: Windows Forms
Published: 23 November 2020
Last Updated: 23 November 2020
Hits: 1813
One my example of adding items to Infragistics UltraGrid:

private void sdnButton1_Click(object sender, EventArgs e)
{
	List<MyChild> stankosKids = new List<MyChild>();
	stankosKids.Add(new MyChild { Name = "Velimir", Gender = "Male" });
	stankosKids.Add(new MyChild { Name = "Hilda", Gender = "Female" });

	List<MyChild> arnoldsKids = new List<MyChild>();
	arnoldsKids.Add(new MyChild { Name = "Thomas", Gender = "Male" });
	arnoldsKids.Add(new MyChild { Name = "Sabrina", Gender = "Female" });

	List<MyChild> chucksKids = new List<MyChild>();
	chucksKids.Add(new MyChild { Name = "Bruce", Gender = "Male" });
	chucksKids.Add(new MyChild { Name = "Lee", Gender = "Female" });

	List<MyParent> list = new List<MyParent>();
	list.Add(new MyParent { ID = 1, FirstName = "Stanko", LastName = "Milosev", Address = "Herseler strasse 8", MyKids = stankosKids });
	list.Add(new MyParent { ID = 2, FirstName = "Arnold", LastName = "Schwarzeneger", Address = "Whitehouse 1", MyKids = arnoldsKids });
	list.Add(new MyParent { ID = 3, FirstName = "Chuck", LastName = "Norris", Address = "Las Vegas", MyKids = chucksKids });

	ultraGrid1.SetDataBinding(list, null);
}

public class MyParent
{
	public int ID { get; set; }
	public string FirstName { get; set; }
	public string LastName { get; set; }
	public string Address { get; set; }

	public List<MyChild> MyKids { get; set; }
}

public class MyChild
{
	public string Name { get; set; }
	public string Gender { get; set; }
}

Image opacity

Details
Written by: Stanko Milosev
Category: Windows Forms
Published: 29 June 2020
Last Updated: 29 June 2020
Hits: 2210
One exampple of how to change image opacity:
private Image SetImageOpacity(Image image, float opacity)
{
	try
	{
		//create a Bitmap the size of the image provided  
		Bitmap bmp = new Bitmap(image.Width, image.Height);

		//create a graphics object from the image  
		using (Graphics gfx = Graphics.FromImage(bmp))
		{

			//create a color matrix object  
			ColorMatrix matrix = new ColorMatrix();

			//set the opacity  
			matrix.Matrix33 = opacity;

			//create image attributes  
			ImageAttributes attributes = new ImageAttributes();

			//set the color(opacity) of the image  
			attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

			//now draw the image  
			gfx.DrawImage(image
				, new Rectangle(0
					, 0
					, bmp.Width
					, bmp.Height
				)
				, 0
				, 0
				, image.Width
				, image.Height
				, GraphicsUnit.Pixel
				, attributes
			);
		}

		return bmp;
	}
	catch (Exception ex)
	{
		MessageBox.Show(ex.Message);
		return null;
	}
}
My example how use this method:
try
{
	float opacity = (float) trackBar1.Value / 100;
	panel1.BackgroundImageLayout = ImageLayout.Center;
	panel1.BackgroundImage = SetImageOpacity(myBitmap, opacity);
}
catch (Exception exception)
{
	MessageBox.Show($@"Cannot to set opacity of picture. Error message: {exception.Message}");
}
Notice that opacity is float and it can be between 0 to 1, everything bigger then 1 will have no impact. Example in Winforms you can download from here.

Taken from here.

  1. Inheriting Windows Forms with Visual C# .NET

Page 2 of 3

  • 1
  • 2
  • 3