C#一行代码之获取控件值

代码使用

var cellValue = dataGridView.ExInvokeGetCellValue(0,1);

代码封装

复制代码
/// <summary>
/// DataGridView获取单元格值
/// </summary>
/// <param name="dataGridView"></param>
/// <param name="rowIndex"></param>
/// <param name="colIndex"></param>
/// <returns></returns>
public static object ExInvokeGetCellValue(this DataGridView dataGridView, int rowIndex, int colIndex)
{
    Func<DataGridView, int, int, object> func = new Func<DataGridView, int, int, object>((gridView, rIndex, cIndex) => gridView.Rows[rIndex].Cells[cIndex].Value);
    if (dataGridView.InvokeRequired)
    {
        IAsyncResult ia = dataGridView.BeginInvoke(func, new object[]
        {
            dataGridView,
            rowIndex,
            colIndex
        });
        return dataGridView.EndInvoke(ia);
    }

    return func.Invoke(dataGridView, rowIndex, colIndex);
}
复制代码

同样的模式可以套用到其他的控件

posted @   懒树懒  阅读(145)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示