我有一个 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。我怎么做?
我有一个数据网格视图,如下所示:
我想:
当表单加载时,如果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)