简体   繁体   English

在 DataGridView c# 中添加列和行

[英]Add Column and Rows in DataGridView c#

I have actually a datagridview loaded with sql query.我实际上有一个加载了 sql 查询的 datagridview。 i want to add a new column with some rows without clean all the data.我想在不清理所有数据的情况下添加一个包含一些行的新列。

The values which I will insert in the rows are string in a list.我将在行中插入的值是列表中的字符串。 And this list is loaded by an sql query made by the first column of the datagridview already loaded.此列表由已加载的 datagridview 的第一列进行的 sql 查询加载。

So i wanted to do basically:所以我基本上想做:

Seals_DataGridView.Columns.Add("column_Type", "Title Column");

        Seals_DataGridView.Rows.Add("GOGO"); 

But obviously this one doesn't work但显然这个不起作用

Anyone have and idea to help me?任何人都有帮助我的想法吗?

Thanks谢谢

If you want to add rows from code try using DataTable and DataGridViewDataSource如果要从代码中添加行,请尝试使用 DataTable 和 DataGridViewDataSource

string constring = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass@123";
        using (SqlConnection con = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
            {
                cmd.CommandType = CommandType.Text;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        dataGridView1.DataSource = dt;
                    }
                }
            }

and other best way is to use Grapically和其他最好的方法是使用 Grapically

Comming to your problem来解决你的问题

Seals_DataGridView.Columns.Add("column_Type", "Title Column");
        Seals_DataGridView.Rows.Add("GOGO"); 

Code you given worked perfectly for me i don't know what the issue with your code you have to elaborate您提供的代码对我来说非常有效,我不知道您的代码有什么问题需要详细说明

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

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