我正在开发一个 C# 应用程序,需要为用户提供一个用户友好的工作环境。这个想法是用户使用他/她的 ID 和密码,因此只有与用户相关的数据才会显示在一张 table 。我已经弄清楚如何做到这一点,但现在用户需要能够编辑表格的内容,因为这一切都是为了给用户提供友好的界面。
我应该使用什么类似表格的组件来实现此目的?我需要能够加载数据并将编辑后的数据保存回数据库中,但我还需要能够为各个单元格和边框分配颜色,以使其看起来更加用户友好。另外,我更喜欢通过单击并编辑的方式来编辑表格内容。
我已经尝试使用 DataGridView,但它在自定义其外观方面表现不佳。
还有其他好的 .net 组件可以用来实现此目的吗?
<小时/>编辑:它是一个 Windows 窗体桌面应用程序。
最佳答案
将单元格绑定(bind)到数据网格时,使用 CellFormatting 事件更改单个单元格
我自己的代码示例:
private void dgColorCodeKey_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex >= 0)
{
//if we are binding a cell in the Color column
if (e.ColumnIndex == colColor.Index)
{
//ColorCode is a custom object that contains the name of a System.Drawing.Color
ColorCode bindingObject = (ColorCode)dgColorCodeKey.Rows[e.RowIndex].DataBoundItem;
dgColorCodeKey[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.FromName(bindingObject.Color);
//set this property to true to tell the datagrid that we've applied our own formatting
e.FormattingApplied = true;
}
}
}
此外,请参阅此 Data Grid FAQ对于数据网格如何工作以及各种方法和事件的作用有一个非常好的、清晰的描述。
关于c# - 显示 MySql 数据的表,用户可以编辑该数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1707779/