简体   繁体   English

在数据网格视图中为每一行添加一个额外的列,并为其赋值

[英]Adding an extra column with a value for each row in a data grid view

I have a DataGridView called dataGridView1 that populates on form load. 我有一个名为dataGridView1的DataGridView,它在表单加载时填充。 How do I add an extra column to the end with a date time stamp of "now" and make sure this Created column is on the end or the row? 如何在日期时间戳记为“ now”的末尾添加额外的列,并确保此Created列在末尾或行中?

I have tried: 我努力了:

dataGridView1.Columns.Add("Created","GETDATE()");

I populate the dataGridView1 like this: 我像这样填充dataGridView1:

private void DisplayAppropriateMessage(FacebookOAuthResult facebookOAuthResult)
{
    if (facebookOAuthResult != null)
    {
        if (facebookOAuthResult.IsSuccess)
        {
            var fb = new FacebookClient(facebookOAuthResult.AccessToken);

            dynamic result = fb.Get("/me");
            var name = result.name;

            {

                var query = string.Format(@"SELECT uid, username, first_name, last_name, friend_count, pic_big 
                        FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())");

                dynamic parameters = new ExpandoObject();
                parameters.q = query;
                dynamic results = fb.Get("/fql", parameters);

                List<MyFriends> q = JsonConvert.DeserializeObject<List<MyFriends>>(results.data.ToString());

                dataGridView1.DataSource = q;
       }
        }
        else
        {
            MessageBox.Show(facebookOAuthResult.ErrorDescription);
        }
    }
}

List<MyFriends> q contains data returned by facebook. List<MyFriends> q包含facebook返回的数据。

You store it as MyFriends type (which BTW should be called MyFriend because each object stores data about one of your friends). 您将其存储为MyFriends类型(应该将其称为MyFriend因为每个对象都存储有关您的一个朋友的数据)。 Then you display this list of friends in datagridview . 然后,在datagridview显示此朋友列表。 I don't know how you configured gridview but If I remember well it can create columns from displayed object's properties automatically or you can define them on your own. 我不知道如何配置gridview,但如果我记得很好,它可以根据显示对象的属性自动创建列,也可以自己定义它们。

So solution is to add property to MyFriends type with name Created and after you get results from Facebook just iterate over results and set their Created property to DateTime.Now . 因此,解决方案是将属性添加为MyFriends类型,名称为Created ,从Facebook获得结果后,只需遍历结果并将其Created属性设置为DateTime.Now Depending on how you configured datagridview you will have to add column to it or not. 根据您配置datagridview您是否必须向其添加列。

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

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