简体   繁体   English

如何将列添加到 DataGrid

[英]How to add Column to DataGrid

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.我搜索了将近 5 个小时,但我找不到任何解决方案。我使用 Visual Studio 2022、wpf 应用程序、c#、xaml。 if is there a proper video can u link it.如果有合适的视频,你可以链接它。

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();因此,您有不同的选项可以将列添加到 C# 中的 DataGridView,最简单的一种是使用DatagridView.Columns.Add(0, Something)如果您要逐个搜索,也可以在 for 中使用它尽可能多地执行此操作,或者如果要指定一种列类型,则可以使用不同的选项,例如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 . ,此外,如果您只想从数据库表中获取所有信息,则可以使用DataGridView.DataSoure = Something.DoSomething Here's an example of what will do that function:这是 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.我真的不知道这是否能回答你的问题,但我希望它会,而且很奇怪你没有找到任何可以帮助你的东西,因为这不是一个真正的难题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM