如何将左侧的类型转换为字符串。实际上我想计算包含值“EBC”的​​行。 [英] How to typecast the left hand side to string. Actually I want to count the row which contains the value "EBC"

查看:93
本文介绍了如何将左侧的类型转换为字符串。实际上我想计算包含值“EBC”的​​行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  for  int  i =  0 ; i <  dataGridView1.Rows.Count; i ++)
{
if (dataGridView1.Rows [i] .Cells [ 公司 ] .Value == EBC
{
int rowcount = dataGridView1.Rows [i] .Cells.Count;
label6.Text = Convert.ToString(rowcount);
}
}





我的尝试:



我想计算具有EBC值的行并将其打印在标签上。所以请帮助...

解决方案

您发布的代码不计行行数,它计算该行中具有EBC值的单元格数。这是你想要的还是以下准确的?



如果你试图在循环时显示行数,你只需在if语句中将rowcount增加1,然后将标签文本设置为该字符串。注意使用.ToString()而不是Convert,虽然它们会产生相同的结果。

  int  rowCount =  0 ; 

for int i = 0 ; i < dataGridView1.Rows.Count; i ++){
if (dataGridView1.Rows [i] .Cells [ 公司]。值== EBC){
rowCount ++;
label6.Text = rowCount.ToString();
}
}





否则你可以完成循环和检查,递增rowCount然后只需设置标签一旦结束。

  int  rowCount =  0 ; 

for int i = 0 ; i < dataGridView1.Rows.Count; i ++){
if (dataGridView1.Rows [i] .Cells [ 公司]。值== EBC){
rowCount ++;
}
}

label6.Text = rowCount.ToString();


另一种方法(不一定更好)是将datagridview的列转换为List例如



  const   int  colWithEbc =  1 ; 

var list = new List< string>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells [colWithEbc] .Value!= null
list.Add( row.Cells [colWithEbc] .Value.ToString());
}

列表< string> list = dataGridView1.Rows 
.OfType< DataGridViewRow>()
.Where(x = > x.Cells [ 1 ]。值!= null
。选择(x = > x.Cells [ 1 ]。Value.ToString())
.ToList();



然后只计算列表中符合条件的项目,例如

 label6.Text = list.Count(n = >  n.ToString()。Equals(  EBC))的ToString(); 


for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if(dataGridView1.Rows[i].Cells["Company"].Value == "EBC")
                {
                    int rowcount = dataGridView1.Rows[i].Cells.Count;
                    label6.Text = Convert.ToString(rowcount);
                }
            }



What I have tried:

I want to count the row which has the value "EBC"and print it on the label. so Please help...

解决方案

The code you posted does not count rows, its counting the number of cells in that row that has the EBC value. Is that what you want or is the below accurate?

If your trying to show the rowcount while looping you just increase the rowcount by 1 inside the if statement, and then set the label text to that number as a string. Note the use of .ToString() instead of Convert, though they produce the same result.

int rowCount = 0;

for (int i = 0; i < dataGridView1.Rows.Count; i++){
    if(dataGridView1.Rows[i].Cells["Company"].Value == "EBC"){
        rowCount++;
        label6.Text = rowCount.ToString();
    }
}



Otherwise you can do the full looping and checks, incrementing the rowCount and then just set the label once at the end.

int rowCount = 0;

for (int i = 0; i < dataGridView1.Rows.Count; i++){
    if(dataGridView1.Rows[i].Cells["Company"].Value == "EBC"){
        rowCount++;
    }
}

label6.Text = rowCount.ToString();


An alternative approach (not necessarily better) is to convert the column of the datagridview to a List e.g.

const int colWithEbc = 1;

var list = new List<string>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
       if (row.Cells[colWithEbc].Value != null)
             list.Add(row.Cells[colWithEbc].Value.ToString());
}

or

List<string> list = dataGridView1.Rows
            .OfType<DataGridViewRow>()
            .Where(x => x.Cells[1].Value != null)
            .Select(x => x.Cells[1].Value.ToString())
            .ToList();


And then just count the items in the list that fit the criteria e.g.

label6.Text  = list.Count(n => n.ToString().Equals("EBC")).ToString();


这篇关于如何将左侧的类型转换为字符串。实际上我想计算包含值“EBC”的​​行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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