如何在datagridview单元格值中获取字符计数 [英] How to get character count in a datagridview cell value

查看:139
本文介绍了如何在datagridview单元格值中获取字符计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取datagridview单元格值中的字符数.

How do you get the number of characters in datagridview cell value.

推荐答案

您需要访问该特定单元格-例如DataGridView.Rows[index].Cells["CellName"].Value;.
然后您可以使用字符串方法获取长度,例如Convert.ToString(DataGridView.Rows[index].Cells["CellName"].Value).Length;.
You need to access that particular cell - For e.g. DataGridView.Rows[index].Cells["CellName"].Value; .
Then you can just use string methods to get the length e.g. Convert.ToString(DataGridView.Rows[index].Cells["CellName"].Value).Length;.


此示例向您展示如何对选定的单元格进行操作:

This example show you how to do it for the selected cell:

private void myDataGridView_SelectionChanged(object sender, EventArgs e)
    {
    DataGridView dgv = sender as DataGridView;
    if (dgv != null)
        {
        DataGridViewSelectedCellCollection selected = dgv.SelectedCells;
        if (selected != null && selected.Count > 0)
            {
            string s = selected[0].Value as string;
            if (s != null)
                {
                Console.WriteLine(s.Length);
                }
            }
        }
    }


这篇关于如何在datagridview单元格值中获取字符计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆