fix UserControlTable

This commit is contained in:
DavidMakarov 2024-10-04 11:42:32 +04:00
parent e3016a6587
commit 8ab777b3b7
2 changed files with 5 additions and 10 deletions

View File

@ -12,12 +12,6 @@ namespace Components.Visual
public int Width { get; set; } public int Width { get; set; }
public bool Visible { get; set; } public bool Visible { get; set; }
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public ColumnInfo(string headerText, string name, int width, bool visible) public string DataPropertyName { get; set; } = string.Empty;
{
HeaderText = headerText;
Name = name;
Width = width;
Visible = visible;
}
} }
} }

View File

@ -42,6 +42,7 @@ namespace Components
HeaderText = columnInfo[i].HeaderText, HeaderText = columnInfo[i].HeaderText,
Width = columnInfo[i].Width, Width = columnInfo[i].Width,
Visible = columnInfo[i].Visible, Visible = columnInfo[i].Visible,
DataPropertyName = columnInfo[i].DataPropertyName,
}; };
dataGridViewTable.Columns.Add(column); dataGridViewTable.Columns.Add(column);
} }
@ -62,8 +63,8 @@ namespace Components
int newRowInd = dataGridViewTable.Rows.Add(); int newRowInd = dataGridViewTable.Rows.Add();
foreach (DataGridViewColumn col in dataGridViewTable.Columns) foreach (DataGridViewColumn col in dataGridViewTable.Columns)
{ {
var prop = props.FirstOrDefault(x => x.Name == col.Name) var prop = props.FirstOrDefault(x => x.Name == col.DataPropertyName)
?? throw new InvalidOperationException($"No property {col.Name} found in type {typeof(T).Name}"); ?? throw new InvalidOperationException($"No property {col.DataPropertyName} found in type {typeof(T).Name}");
var val = prop.GetValue(value); var val = prop.GetValue(value);
dataGridViewTable.Rows[newRowInd].Cells[col.Index].Value = val; dataGridViewTable.Rows[newRowInd].Cells[col.Index].Value = val;
} }
@ -87,7 +88,7 @@ namespace Components
for (int i = 0; i < dataGridViewTable.ColumnCount; i++) for (int i = 0; i < dataGridViewTable.ColumnCount; i++)
{ {
var curCell = cells[i]; var curCell = cells[i];
var prop = props.FirstOrDefault(x => x.Name == dataGridViewTable.Columns[i].Name); var prop = props.FirstOrDefault(x => x.Name == dataGridViewTable.Columns[i].DataPropertyName);
prop?.SetValue(returnObject, curCell.Value); prop?.SetValue(returnObject, curCell.Value);
} }
return returnObject; return returnObject;