首页 > 解决方案 > 如何从数据库中的列中计算字符长度并将它们放入 datagridview 中显示的新列中

问题描述

基本上,我有一个数据库,其中包含一个名为 anuncios 的表,其中包含以下列:

idAnnouncements | title | message | idClients (foreign key)|

,并且我有另一个名为 detalhefatura 的表,其中包含以下列:

idAnnouncements | idFatura (foreign  key) | subTotal

在 C# 中:当我单击一个按钮时,我想计算 Announcements 表中消息列中的字母,并将它们放入列 subTotal 中,像这样

http://prntscr.com/mbijxs

是需要查询还是只需要 C# 代码?

这就是我得到的,在上面打印屏幕中看到的标签中,具有相同 ClientID 的所有字符的数量

private void button1_Click(object sender, EventArgs e)
    {

        {
            var linhas = dataGridView.Rows.Cast<DataGridViewRow>().Select(r => Convert.ToString(r.Cells["mensagem"].Value));
            double somaLinhas = linhas.Sum(r => r.Length * 0.02);
            lbltotal.Text = string.Format(@"{0:#,##0.00}", somaLinhas);


        }

    }

标签: c#mysqldatagridview

解决方案


      columnSize = (Int32)cmd.ExecuteScalar();

在这之后:

dataGridView1.Rows[rowIndex].Cells[columnIndex].Value = columnSize;

推荐阅读