小编Min*_*iyo的帖子

如何在不作为引用的情况下克隆通用 List<T> ?

我有一个 C# 对象的通用列表,并且希望克隆该列表。

List<Student> listStudent1 = new List<Student>();
List<Student> listStudent2 = new List<Student>();
Run Code Online (Sandbox Code Playgroud)

我使用了下面的扩展方法,但它不能:(当 listStudent2 发生变化时 -> 影响 listStudent1)

public static List<T> CopyList<T>(this List<T> oldList)
{
    var newList = new List<T>(oldList.Capacity);
    newList.AddRange(oldList);

    return newList;
}
Run Code Online (Sandbox Code Playgroud)

我想继续添加元素或在 listStudent2 中进行更改而不影响 listStudent1。我怎么做?

c# generics clone

3
推荐指数
1
解决办法
7652
查看次数

如何根据 Combobox 的值更改 DataGridView 单元格颜色?

我有一个数据网格视图,如下所示:

在此处输入图片说明

我想:

  • 当表单加载时,如果Gender列的值为男性,则列对应的颜色单元格Name将为白色

  • 何时更改列的值Gender:Male ?女性,列的颜色单元格Name将为深灰色,否则如果更改列的值Gender:女性?男性,列的颜色单元格Name将为白色

我试过了,但我无法做到:

    private void dataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        DataGridView dgv = sender as DataGridView;
        DataGridViewCell cell = dgv.CurrentCell;

        if (dgv.Rows[cell.RowIndex].Cells["Gender"].Value.ToString().Trim() == "Male")
        {
            // Male
            dgv.Rows[cell.RowIndex].DefaultCellStyle.BackColor = Color.White;
        }
        else
        {
            // Female
            dgv.Rows[cell.RowIndex].DefaultCellStyle.BackColor = Color.DarkGray;
        }
    }
Run Code Online (Sandbox Code Playgroud)

或者:

    private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        DataGridView dgv = sender as DataGridView;

        if (dgv.Columns[e.ColumnIndex].Name.Equals("Gender"))
        {
            if (e.Value != null && e.Value.ToString().Trim() == "Male") …
Run Code Online (Sandbox Code Playgroud)

c# datagridviewcombobox

3
推荐指数
1
解决办法
6435
查看次数

标签 统计

c# ×2

clone ×1

datagridviewcombobox ×1

generics ×1