就像标题所说,我希望能够在List
的DataGridView中添加按钮。
到目前为止我所拥有的:
public class Users
{
public Int64 UserID { get; set; }
public DataGridViewButtonColumn EmailAddress { get; set; }
public DataGridViewButtonColumn Gamertag { get; set; }
public DataGridViewButtonColumn Xuid { get; set; }
public String SignedIn { get; set; }
public String AutoSignin { get; set; }
public Image Gamerpic { get; set; }
}
private List<Users> GetUsersList()
{
try
{
List<Users> list = new List<Users>();
IEnumerable<XboxUser> users = _xbc.Users;
foreach (XboxUser user in users)
{
DataGridViewButtonColumn EmailAddress = new DataGridViewButtonColumn()
{
Text = user.EmailAddress
};
DataGridViewButtonColumn Gamertag = new DataGridViewButtonColumn()
{
Text = user.GamerTag
};
DataGridViewButtonColumn Xuid = new DataGridViewButtonColumn()
{
Text = user.Xuid
};
Users xbuser = new Users()
{
UserID = user.UserId,
EmailAddress = EmailAddress,
Gamertag = Gamertag,
Xuid = Xuid,
SignedIn = user.IsSignedIn.ToString(),
AutoSignin = user.AutoSignIn.ToString(),
Gamerpic = GetImageFromUrl(Gamertag.Text)
};
list.Add(xbuser);
}
return list;
}
catch (Exception ex)
{
}
return null;
}
private void Bo_Refresh_Click(object sender, EventArgs e)
{
List<Users> listUsers = GetUsersList();
Dg_UserList.DataSource = new BindingSource(new BindingList<Users>(listUsers), null);
}
private void Dg_UserList_CellClick(object sender, DataGridViewCellEventArgs e)
{
switch (e.ColumnIndex)
{
case 1:
Clipboard.SetText(Dg_UserList.Rows[e.RowIndex].Cells["EmailAddress"].Value.ToString());
MessageBox.Show("Email " + Dg_UserList.Rows[e.RowIndex].Cells["EmailAddress"].Value.ToString() + " has been copied in the clipboard.", "Copy to Clipboard", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case 2:
Clipboard.SetText(Dg_UserList.Rows[e.RowIndex].Cells["Gamertag"].Value.ToString());
MessageBox.Show("Gamertag " + Dg_UserList.Rows[e.RowIndex].Cells["Gamertag"].Value.ToString() + " has been copied in the clipboard.", "Copy to Clipboard", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case 3:
Clipboard.SetText(Dg_UserList.Rows[e.RowIndex].Cells["Xuid"].Value.ToString());
MessageBox.Show("Xuid " + Dg_UserList.Rows[e.RowIndex].Cells["Xuid"].Value.ToString() + " has been copied in the clipboard.", "Copy to Clipboard", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
所有内容都正确显示(甚至是Gamerpic中的图片),但按钮除外,它显示DataGridViewButtonColumn { Name=, Index=-1 }
而不是带有正确值的按钮。
我也尝试使用DataGridVewButtonCell
,但也无效。
但是,当我点击一个应该是按钮的单元格时,Clipboard.SetText
可以正常工作,但始终会将DataGridViewButtonColumn { Name=, Index=-1 }
放入其中。
编辑: 我已经能够用字符串代替它...看起来不那么用户友好,因为没有按钮告诉你可以点击它...但它的工作原理!我确定我更喜欢按钮:)
答案 0 :(得分:3)
<强>问题强>
当DataSourced时,DataGridView
将根据每列的数据类型自动创建列。默认情况下,此处的每个列都创建为DataGridViewTextBoxColumn
,但绑定到Image
的列除外 - 默认为DataGridViewImageColumn
。即使您有三个DataGridViewButtonColumn
属性,它们也会绑定到文本列 - 这将设置值如下(示例):
NewRow.Cells[1].Value = DataSourceObject.EmailAddress.ToString();
// I.E. EmailAddress is a DataGridViewButtonColumn, so...
NewRow.Cells[1].Value = DataGridViewButtonColumn.ToString();
Console.WriteLine(DataGridViewButtonColumn.ToString()); // prints "DataGridViewButtonColumn { Name=, Index=-1 }"
<强>解决方案强>
从Users
类开始:将所有DatagridViewButtonColumn
属性更改为键入string
。
public class Users
{
public Int64 UserID { get; set; }
public String EmailAddress { get; set; }
public String Gamertag { get; set; }
public String Xuid { get; set; }
public String SignedIn { get; set; }
public String AutoSignin { get; set; }
public Image Gamerpic { get; set; }
}
并更改您的GetUserList
方法以解决差异:
private List<Users> GetUsersList()
{
try
{
List<Users> list = new List<Users>();
IEnumerable<XboxUser> users = _xbc.Users;
foreach (XboxUser user in users)
{
Users xbuser = new Users()
{
UserID = user.UserId,
EmailAddress = user.EmailAddress,
Gamertag = user.Gamertag,
Xuid = user.Xuid,
SignedIn = user.IsSignedIn.ToString(),
AutoSignin = user.AutoSignIn.ToString(),
Gamerpic = GetImageFromUrl(Gamertag.Text)
};
list.Add(xbuser);
}
return list;
}
catch (Exception ex)
{
}
return null;
}
最后,在您的Form构造函数或Load事件中 - 在绑定数据之前 - 手动添加列,如下所示:
Dg_UserList.AutoGenerateColumns = false;
DataGridViewTextBoxColumn userCol = new DataGridViewTextBoxColumn();
DataGridViewButtonColumn emailCol = new DataGridViewButtonColumn();
DataGridViewButtonColumn tagCol = new DataGridViewButtonColumn();
DataGridViewButtonColumn xuidCol = new DataGridViewButtonColumn();
DataGridViewTextBoxColumn signCol = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn autoCol = new DataGridViewTextBoxColumn();
DataGridViewImageColumn picCol = new DataGridViewImageColumn();
userCol.Name = "UserID"; // Allows access to columns or
emailCol.Name = "EmailAddress"; // cells in the following manner:
tagCol.Name = "Gamertag"; // dgv.Rows[index].Cells["Name"];
xuidCol.Name = "Xuid";
signCol.Name = "SignedIn";
autoCol.Name = "AutoSignin";
picCol.Name = "Gamerpic";
userCol.DataPropertyName = "UserID"; // MUST match DataSource properties.
emailCol.DataPropertyName = "EmailAddress"; // Allows DataSourced columns to match
tagCol.DataPropertyName = "Gamertag"; // up with manually created columns.
xuidCol.DataPropertyName = "Xuid";
signCol.DataPropertyName = "SignedIn";
autoCol.DataPropertyName = "AutoSignin";
picCol.DataPropertyName = "Gamerpic";
userCol.HeaderText = "User ID"; // Allows displaying different text
emailCol.HeaderText = "Email Address"; // for the column headers.
tagCol.HeaderText = "Gamer Tag";
xuidCol.HeaderText = "Xuid";
signCol.HeaderText = "Signed In";
autoCol.HeaderText = "Auto Sign in";
picCol.HeaderText = "Avatar";
Dg_UserList.Columns.Add(userCol);
Dg_UserList.Columns.Add(emailCol);
Dg_UserList.Columns.Add(tagCol);
Dg_UserList.Columns.Add(xuidCol);
Dg_UserList.Columns.Add(signCol);
Dg_UserList.Columns.Add(autoCol);
Dg_UserList.Columns.Add(picCol);
这将允许您创建按钮列,但仍允许您绑定数据而不是手动添加每一行。感谢DataPropertyName
,按钮单元格将正确显示绑定的单元格值。