fix UserControlTable

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

View File

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

View File

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