简体   繁体   中英

Accessing RowCount from another class

I have a winform, that takes a number of params, executes an stored procedure and then populates a DataGridView with the results.

What I'd like to do is add a string the bottom of my winform that returns a message, including a count of total rows returned.

This presently works fine, using the following syntax :

deskSearchResultCount.Text = String.Format("Your search returned {0} results", deskDataGridView.RowCount );

However as the majority of my application has been written out in the initial form, I was moving sections into classes, to 'tidy it up' a bit - (I'm still very new to c# so apologies if this is a n00b mistake)

Once I add a class called Summary, I would like to call my RowCount as follows :

Summary Returned = new Summary();

            deskSearchResultCount.Text = String.Format("Your search returned {0} results", Returned.Total("Desk"));

With my Summary class containing the following :

    public class Summary : ContactAnalysisToolbox
{
    public int Total(string type)
    {
        if (type == "Desk")
        {
            return deskDataGridView.Rows.Count;
        }
        else
        {
            return visionDataGridView.Rows.Count;
        }
    }

However the returned count is always 0. When stepping through the process also, at no point does it appear to be attempting to set itself as anything different.

Can anyone please help / point me in the right direction?

Thanks.

Edit -

I've included the full form also -

    using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.DirectoryServices.AccountManagement;
using System.IO;

//
//  Todo :
//      -- Capture Assigned Team In Output
//
//

namespace CallsAnalysisToolbox
{
    public partial class ContactAnalysisToolbox : Form
    {
        public ContactAnalysisToolbox()
        {
            InitializeComponent();

            // Grabs username for current user and displays a welcome message
            this.welcomeMessage.Text = "Welcome " + UserPrincipal.Current.Name;
        }

        private void searchVision_Click(object sender, EventArgs e)
        {
            try
            {
                // Run sp_incomingCalls on SQLCLUSTER
                this.rep_IncomingCallsTableAdapter.Fill(this.visionReportsDataSet.Rep_IncomingCalls, dateTimePicker1.Value, dateTimePicker2.Value, textBox1.Text, textBox2.Text);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

            // Make export button active when search returns results
            exportVisionButton.Enabled = (visionDataGridView.Rows.Count > 0);

            // Assign returned row count to visionSearchResultCount label and then display label
            visionSearchResultCount.Text = String.Format("Your search returned {0} results", visionDataGridView.RowCount);
            visionSearchResultCount.Visible = true;
        }

        private void searchDesk_Click(object sender, EventArgs e)
        {
            try
            {
                // Run sp_caseActivity on SQLCLUSTER
                this.rPT_CaseActivityTableAdapter.Fill(this.deskDataSet.RPT_CaseActivity, deskFrom.Value, deskTo.Value, deskClientList.Text, deskBenefitList.Text, deskStatusList.Text);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

            if (deskDataGridView.Rows.Count > 0)
            {


                exportDeskButton.Enabled = true;
                deskSearchResultCount.Visible = true;

                Summary Returned = new Summary();

                deskSearchResultCount.Text = String.Format("Your search returned {0} results", Returned.Total("Desk"));

                deskSummaryData.Visible = true;
                noDataDesk.Visible = false;

                // Populate the summary tab

                // Get Email / Phone case count


                deskTotalCaseResults.Text = deskDataGridView.RowCount.ToString();
                //deskTotalEmailCasesResults.Text = emailCount.ToString();
                //deskTotalPhoneCasesResults.Text = phoneCount.ToString();
            }
        }

        //TODO : Combine Export functions. Ideally just a single function rather than the repeated logic within the Export class for each datagrid
        private void exportVisionButton_Click(object sender, EventArgs e)
        {
            Export Export = new Export();

            Export.ReturnedResult("Vision");
        }

        private void exportDeskButton_Click(object sender, EventArgs e)
        {
            Export Export = new Export();

            Export.ReturnedResult("Desk");
        }

        private void deskDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // Limit 'open case in browser' action to first cell
            if (deskDataGridView.CurrentCell.ColumnIndex.Equals(0))
            {
                string url = string.Format("https://qa.internal.local/agent/case/{0}", deskDataGridView.Rows[e.RowIndex].Cells[0].Value);
                Process.Start(url);
            }
        }
    }
}

For example you could do something like this. It's called Method Extension

Try this

public static class Helpers
{
    public static string ToTotalCount(this DataGridView item)
    {
        return string.Format("Your search returned {0} results", item.Rows.Count);
    }
}

In your form make sure that the namespace of the class Helpers is in the usings:

 deskSearchResultCount.Text = deskDataGridView.ToTotalCount();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM