簡體   English   中英

用 C# 在我的 datagridview 的列中計算“✓”的數量

[英]Counting the number of "✓" in a column in my datagridview with C#

我真的想弄清楚如何在性能監視器的列中計算“✓”這個符號。 我真的無法弄清楚這個代碼......我嘗試了很多可能的方法,但沒有運氣。 在visual basic中我設法做到了,但在C#中,我就是想不通。

這是我嘗試過的一些代碼示例。

var count = dataGridView1.Rows.Cast<DataGridViewRow>()
    .Where(row => !(row.Cells[7].Value == null || row.Cells[7].Value == DBNull.Value))
    .Count();

txtSearch.Text = count.ToString();
int sum = 0;
for (int i = 0; i <= dataGridView1.Rows.Count(); i++)
{
    sum = int.Parse(dataGridView1.Rows[i].Cells[7].Value.ToString());
}
txtSearch.Text = sum.ToString();
txtSearch.Text = (from DataGridViewRow row in dataGridView1.Rows
        where row.Cells[7].FormattedValue.ToString() != string.Empty
        select Convert.ToInt32(row.Cells[7].FormattedValue)).Sum().ToString();

@demo您好,抱歉缺少信息,我有點嘗試這就是為什么也是總和,我需要計算次數是✓- 在該列中並在文本框中獲取值。

@igor 我正在發布 vb 代碼。 也因為缺乏信息而感到抱歉。 我得到的錯誤..通常我什么也沒得到。 它只是不起作用......

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    Form15.Show()
    Dim colsum As Decimal
    Dim colsum2 As Decimal
    Dim colsum3 As Decimal
    Dim colsum4 As Decimal
    Dim colsum5 As Decimal
    Dim colsum6 As Decimal
    Dim colsum7 As Decimal
    Dim colsum8 As Decimal

    Dim a As Integer = 7
    Dim b As Integer = 8
    Dim c As Integer = 9
    Dim d As Integer = 10
    Dim x As Integer = 11
    Dim f As Integer = 12
    Dim g As Integer = 13
    Dim h As Integer = 14



    colsum = ((From Rows In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not Rows.IsNewRow AndAlso Rows.Cells(a).Value = "✓").Count / 20) * 100
    colsum2 = ((From Rows In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not Rows.IsNewRow AndAlso Rows.Cells(b).Value = "✓").Count / 20) * 100
    colsum3 = ((From Rows In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not Rows.IsNewRow AndAlso Rows.Cells(c).Value = "✓").Count / 20) * 100
    colsum4 = ((From Rows In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not Rows.IsNewRow AndAlso Rows.Cells(d).Value = "✓").Count / 20) * 100
    colsum5 = ((From Rows In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not Rows.IsNewRow AndAlso Rows.Cells(x).Value = "✓").Count / 20) * 100
    colsum6 = ((From Rows In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not Rows.IsNewRow AndAlso Rows.Cells(f).Value = "✓").Count / 20) * 100
    colsum7 = ((From Rows In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not Rows.IsNewRow AndAlso Rows.Cells(g).Value = "✓").Count / 20) * 100
    colsum8 = ((From Rows In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not Rows.IsNewRow AndAlso Rows.Cells(h).Value = "✓").Count / 20) * 100


    Form15.Label9.Text = colsum
    Form15.Label10.Text = colsum2
    Form15.Label11.Text = colsum3
    Form15.Label12.Text = colsum4
    Form15.Label13.Text = colsum5
    Form15.Label14.Text = colsum6
    Form15.Label15.Text = colsum7
    Form15.Label16.Text = colsum8

    Form15.Chart1.Series("Machine-Load").Points.AddXY("Hard-Milling", Form15.Label9.Text)
    Form15.Chart1.Series("Machine-Load").Points.AddXY("Graphite Milling", Form15.Label10.Text)
    Form15.Chart1.Series("Machine-Load").Points.AddXY("Wire-Cut", Form15.Label11.Text)
    Form15.Chart1.Series("Machine-Load").Points.AddXY("Erosion", Form15.Label12.Text)
    Form15.Chart1.Series("Machine-Load").Points.AddXY("Drilling", Form15.Label13.Text)
    Form15.Chart1.Series("Machine-Load").Points.AddXY("Flat-Grinding", Form15.Label14.Text)
    Form15.Chart1.Series("Machine-Load").Points.AddXY("Profile Grinding", Form15.Label15.Text)
    Form15.Chart1.Series("Machine-Load").Points.AddXY("Laser Engraving", Form15.Label16.Text)
End Sub

在編程中,傾向於將數據(= 模型)與數據的顯示方式(= 視圖)分開。 這樣做的好處是您可以更改顯示數據的方式,而無需更改 model。 因此,如果您決定顯示復選框而不是復選標記,則您的 model 不必更改。

要將 model 轉換為視圖,需要一個適配器 class:ViewModel。 這三個類一起縮寫為 MVVM。 考慮閱讀有關此的一些背景知識。

在 Winforms 中,您可以通過使用 DataGridView 的 DataSource 來使用 ViewModel。

如果您要顯示的數據與您的 model 中的數據非常相似,則不需要特殊的顯示 class。 如果數據完全不同,為顯示數據創建一個特殊的 class 可能是明智之舉。因此,如果要顯示客戶,則創建一個 class 顯示客戶:

class DisplayCustomer
{
    public Customer Customer {get; set;}  // the Customer from the model

    public int Id
    {
        get => this.Customer.Id,
        set => this.Customer.Id = value;
    }

    public string Name
    {
        get => this.Customer.Name,
        set => this.Customer.Name = value,
    }

    ... etc.

但同樣:如果顯示的數據與 model 數據沒有區別,則不需要單獨的 class。

一個很好的例子是你的復選標記。 如果這是 Customer 中的 Boolean,但 DisplayCustomer 中的字符串 Checkmark,您將有如下內容:

class Customer
{
    ...
    public bool IsSpecial {get; set;}
|

class DisplayCustomer
{
    ...
    private const string checkMark = "✓";
    public string IsSpecial
    {
        get => this.Customer.IsSpecial ? checkMark : String.Empty;
        set => this.Customer.IsSpecial = checkMark == value;
    }
 

在 Visual Studio Designer 中,您將添加列。 使用屬性DataGridViewColumn.DataPropertyName定義必須在列中顯示的屬性的名稱。 這可以在設計器或構造器中完成:

class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();

        this.columnId.DataPropertyName = nameof(DisplayCustomer.Id);
        this.columnName.DataPropertyName = nameof(DisplayCustomer.Name);
        ...
        this.columnIsSpecial.DataPropertyName = nameof(DisplayCustomer.IsSpecial);
    }

顯示您的客戶:

public BindingList<DisplayCustomer> DisplayedCustomers
{
    get => (BindingList<DisplayCustomer>)this.dataGridView1.DataSource;
    set => this.dataGridView1.DataSource = value;
}

public void DisplayCustomers()
{
    IEnumerable<Customer> customers = this.FetchCustomers();
    this.DisplayedCustomers = new BindingList<DisplayCustomer(
       customers.Select(customer => new DisplayCustomer {Customer = customer})
                .ToList());
}

就是這樣:兩個幾乎合一的線性函數使您的數據顯示出來,如果操作員添加/刪除/編輯行或單元格,則數據會自動更新。

例如,在編輯后,操作員按下立即應用按鈕。

private void OnButtonApplyNow_Clicked(object sender, ...)
{
    IEnumerable<Customer> editedCustomers = this.DisplayedCustomers
        .Select(displayedCustomer => displayedCustomer.Customer);
    this.ProcessEditedCustomers(editedCustomers);
}

計算您的復選標記現在相當容易:

private int CountCheckMarks(IEnumerable<Customer> customers)
{
    return customers.Where(customer => customer.IsSpecial).Count();
}

或者如果你想要一個屬性:

private int CheckMarkCount => this.DisplayedCustomers
    .Where(customer => customer.IsSpecial).Count()

其他一些改進

除了屬性 DisplayedCustomers,您還可以為當前客戶和選定客戶創建屬性。

public DisplayCustomer CurrentDisplayCustomer =>
    (DisplayCustomer) this.dataGridView1.CurrentRow?.DataBoundItem;

public Customer CurrentCustomer => this.CurrentDisplayCustomer?.Customer;

public IEnumerable<DisplayCustomer> SelectedDisplayCustomers =>
    this.dataGridView1.SelectedRows.Cast<DataGridViewRow>()
        .Select(row => row.DataBoundItem)
        .Cast<DisplayCustomer>();

再說一遍:如果需要,只創建一個特殊的 DisplayCustomer class。

但我不想使用數據源!

有些人喜歡直接訪問 DataGridViewCells。 這個怎么樣:

private const string checkMark = "✓";

private IEnumerable<DataGridViewRow> CheckedRows => this.dataGridView1.Rows
    .Cast<DataGridViewRow>()
    .Where(row => row.Cells[this.columnIsSpecial.DisplayIndex].Value.ToString() == checkMark);

private int CheckedRowCount => this.CheckedRows.Count();

我制作了一個特殊的屬性 CheckedRows,因為我想你可能想將它用於其他目的,然后只計算它們。

感謝大家的幫助,我祝你一切順利。 我通過計算列中的非空單元格來解決我的問題。 也許它可以幫助其他人,我會發布代碼

 int count = dataGridView1.Rows
           .Cast<DataGridViewRow>()
           .Select(row => (string)row.Cells["VCE600PRO"].Value)
            .Where(v => !string.IsNullOrEmpty(v))
           .Count();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM