Home > Software design >  How to add Column to DataGrid
How to add Column to DataGrid

Time:07-27

making a password manager for myself. im searching it almost 5 hours but i couldnt find any solutions.im using Visual Studio 2022, wpf applicaiton, c#, xaml. if is there a proper video can u link it.

CodePudding user response:

So you have diferent options to add columns to a DataGridView in C#, the most simple one is to use DatagridView.Columns.Add(0, Something) if your searching to put them one by one, you can also use it inside a for to do it as many times as you want, or if you want to specify a type of column you have diferent options like DataGridViewButtonColumn btn = new DataGridViewButtonColumn();, also if you only want to take all the information from a table of a database you can use DataGridView.DataSoure = Something.DoSomething. Here's an example of what will do that function:

public static List<Something> DoSomething(MySqlConnection connection)
    {
        List<Something> something = new List<Something>();
        string query = "SELECT * FROM something";
        MySqlCommand command = new MySqlCommand(query, connection);
        MySqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            something.Add(new Something(reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4)));
        }
        reader.Close();
        return something;
    }

I don't really know if this answer your question but I hope it does, and it's quite strange you dind't found anything that help's you out because isn't a really a difficult problem.

  • Related