Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-Datagridview-计算DGV中的记录_C#_Sorting_Datagridview - Fatal编程技术网

C#-Datagridview-计算DGV中的记录

C#-Datagridview-计算DGV中的记录,c#,sorting,datagridview,C#,Sorting,Datagridview,所以我正在做一个学校的项目,我快完成了,但我有一个问题。我的“节目”是为了音乐,我有不同的体裁。我的任务是以某种方式根据音乐对艺术家进行分类,并将其全部计数。我的意思是,当你点击menustrip中的“按流派排序”时,一个消息框会显示“rock 4,pop 3等”,数字指的是艺术家,在Datagridview中有多少。但我不知道如何编写代码。所以,如果你们有一个例子,我真的很高兴知道它。我的代码现在看起来像这样。一些斯洛文尼亚语,但是,不要介意 using System; usi

所以我正在做一个学校的项目,我快完成了,但我有一个问题。我的“节目”是为了音乐,我有不同的体裁。我的任务是以某种方式根据音乐对艺术家进行分类,并将其全部计数。我的意思是,当你点击menustrip中的“按流派排序”时,一个消息框会显示“rock 4,pop 3等”,数字指的是艺术家,在Datagridview中有多少。但我不知道如何编写代码。所以,如果你们有一个例子,我真的很高兴知道它。我的代码现在看起来像这样。一些斯洛文尼亚语,但是,不要介意

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

   namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void konecToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Close(); // ugasnemo okno :)
    }

    private void dateTimePicker1_Validating(object sender, CancelEventArgs e)
    {
        // validacija vnosa datuma, album ne sme biti izdan 2013(ker pač ne sme bit, ne se razubrjat)
        if (dateTimePicker1.Value.Year.ToString() == "2013")
            errorProvider1.SetError(dateTimePicker1, "Year must be earlyer that 2013");
        else
            errorProvider1.SetError(dateTimePicker1, "");
    }

    private void bCancel_Click(object sender, EventArgs e)
    { // brisanje vnosa na panelu
        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();
        comboBox1.Text = "";
        dateTimePicker1.Value = DateTime.Today;
        radioButton1.Checked = false;
        radioButton2.Checked = false;
        radioButton3.Checked = false;
        radioButton4.Checked = false;
    }

    private void bAdd_Click(object sender, EventArgs e)
    {
        //vnašanje iz panela v datagridwiev.
        string Zvrst = "";
        if (radioButton1.Checked)
            Zvrst = radioButton1.Text;
        if (radioButton2.Checked)
            Zvrst = radioButton2.Text;
        if (radioButton3.Checked)
            Zvrst = radioButton3.Text;
        if (radioButton4.Checked)
            Zvrst = radioButton4.Text;
        //napaka če niso vsa polja vnešena !!
        if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || comboBox1.Text == "" || Zvrst == "")
        {
            MessageBox.Show("Prosimo vnesite vrednosti!");
            errorProvider1.SetError(bDodaj, "Prosimo vnesite vrednosti");
        }
        else
        dGVKos.Rows.Add(textBox1.Text, textBox2.Text, textBox3.Text, comboBox1.Text, Zvrst, dateTimePicker1.Value.ToString("dd/MM/yyyy")); 

    }

    private void dGVKos_CellClick(object sender, DataGridViewCellEventArgs e)
    {//deletes current row
        if (e.ColumnIndex == 6)
            dGVKos.Rows.Remove(dGVKos.CurrentRow);
        if (e.ColumnIndex == this.dGVKos.Columns[7].Index)
        {
            textBox1.Text = dGVKos.CurrentRow.Cells[0].Value.ToString();
            textBox2.Text = dGVKos.CurrentRow.Cells[1].Value.ToString();
            textBox3.Text = dGVKos.CurrentRow.Cells[2].Value.ToString();
            comboBox1.Text = dGVKos.CurrentRow.Cells[3].Value.ToString();
            if
            (dGVKos.CurrentRow.Cells[4].Value.ToString() == "Pop") { radioButton1.Checked = true; }
            else { radioButton1.Checked = false; }
            if (dGVKos.CurrentRow.Cells[4].Value.ToString() == "Rock") { radioButton2.Checked = true; } 
            else { radioButton2.Checked = false; }
            if (dGVKos.CurrentRow.Cells[4].Value.ToString() == "Metal") { radioButton3.Checked = true; } 
            else { radioButton3.Checked = false; }
            if (dGVKos.CurrentRow.Cells[4].Value.ToString() == "Slo/YU") { radioButton4.Checked = true; } 
            else { radioButton4.Checked = false; }
            dateTimePicker1.Value = Convert.ToDateTime(dGVKos.CurrentRow.Cells[5].Value);


        }
    }

    private void shraniToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //saves everything to .txt file (lahko uporabimo tudi saveFileDialog
        StreamWriter pisi = File.CreateText("Glasba.txt");
        //preverimo če je kašna vrstica v dGvKos
        if (dGVKos.Rows.Count > 0)
        {
            //premikamo se po vsrsticah in jih zapisujemo v datoteko
            for (int i = 0; i < dGVKos.Rows.Count; i++)
                pisi.WriteLine(dGVKos.Rows[i].Cells[0].Value + " " +
                    dGVKos.Rows[i].Cells[1].Value + " " +
                    dGVKos.Rows[i].Cells[2].Value + " " +
                    dGVKos.Rows[i].Cells[3].Value + " " +
                    dGVKos.Rows[i].Cells[4].Value + " " +
                    dGVKos.Rows[i].Cells[5].Value + " ");
            pisi.Close();
            MessageBox.Show("Glasbeniki so vnešeni v datoteko Glasba.txt v mapi debug");
        } // če ni vrstic se nam izpiše ->
        else
            MessageBox.Show("Ni nobene vrstice");

    }

    private void infoToolStripMenuItem_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Program je namenjen vnašanju najljubših glasbenikov in njihovih albumov. Izvajalce se potem da razvrstit tudi po sami zvrsti s pomočjo postavke v menuju" +
           " " + "V polja vnesemo željene vrednosti in dodamo vnos katerega lahko urejamo, brišemo itd. Pri datumu mora letnica biti manjša od 2013.");
    }

    private void sortBygenreToolStripMenuItem_Click(object sender, EventArgs e)
    {

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.IO;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void konecToolStripMenuItem\u单击(对象发送方,事件参数e)
{
Close();//ugasnemo-okno:)
}
私有无效dateTimePicker1\u验证(对象发送方,CancelEventArgs e)
{
//validacija vnosa datuma,2013年专辑ne sme biti izdan(ker pačne sme bit,ne se razubrjat)
如果(dateTimePicker1.Value.Year.ToString()=“2013”)
errorProvider1.SetError(dateTimePicker1,“年份必须早于2013年”);
其他的
errorProvider1.SetError(dateTimePicker1,“”);
}
私有无效b取消单击(对象发送方,事件参数e)
{//brisanje vnosa na panelu
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
comboBox1.Text=“”;
dateTimePicker1.Value=DateTime.Today;
radioButton1.选中=假;
radioButton2.选中=错误;
radioButton3.选中=错误;
radioButton4.选中=错误;
}
私有void添加单击(对象发送者,事件参数e)
{
//vnašanje iz panela诉datagridwiev。
字符串Zvrst=“”;
如果(radioButton1.选中)
Zvrst=无线电按钮1.文本;
如果(radioButton2.选中)
Zvrst=无线电按钮2.文本;
如果(收音机按钮3.选中)
Zvrst=无线电按钮3.文本;
如果(收音机按钮4.选中)
Zvrst=无线电按钮4.文本;
//napakače niso vsa polja vnešena!!
如果(textBox1.Text==“”| | textBox2.Text==“”| | textBox3.Text==“”| | comboBox1.Text==“”| | Zvrst==“”)
{
MessageBox.Show(“Prosimo vnesite vrednosti!”);
errorProvider1.SetError(bDodaj,“Prosimo vnesite vrednosti”);
}
其他的
dGVKos.Rows.Add(textBox1.Text、textBox2.Text、textBox3.Text、comboBox1.Text、Zvrst、dateTimePicker1.Value.ToString(“dd/MM/yyyy”);
}
私有void dGVKos_CellClick(对象发送方,DataGridViewCellEventArgs e)
{//删除当前行
如果(e.ColumnIndex==6)
dGVKos.Rows.Remove(dGVKos.CurrentRow);
if(e.ColumnIndex==this.dGVKos.Columns[7].Index)
{
textBox1.Text=dGVKos.CurrentRow.Cells[0].Value.ToString();
textBox2.Text=dGVKos.CurrentRow.Cells[1].Value.ToString();
textBox3.Text=dGVKos.CurrentRow.Cells[2].Value.ToString();
comboBox1.Text=dGVKos.CurrentRow.Cells[3].Value.ToString();
如果
(dGVKos.CurrentRow.Cells[4].Value.ToString()=“Pop”){radioButton1.Checked=true;}
else{radioButton1.Checked=false;}
if(dGVKos.CurrentRow.Cells[4].Value.ToString()=“Rock”){radioButton2.Checked=true;}
else{radioButton2.Checked=false;}
if(dGVKos.CurrentRow.Cells[4].Value.ToString()=“金属”){radioButton3.Checked=true;}
else{radioButton3.Checked=false;}
if(dGVKos.CurrentRow.Cells[4].Value.ToString()=“Slo/YU”){radioButton4.Checked=true;}
else{radioButton4.Checked=false;}
dateTimePicker1.Value=Convert.ToDateTime(dGVKos.CurrentRow.Cells[5].Value);
}
}
私有void shraniToolStripMenuItem\u单击(对象发送方,事件参数e)
{
//将所有内容保存到.txt文件(lahko uporabimo tudi saveFileDialog
StreamWriter pisi=File.CreateText(“Glasba.txt”);
//preverimočje kašna vrstica诉dGvKos
如果(dGVKos.Rows.Count>0)
{
//在jih zapisujemo诉datoteko案中,premikamo在第二次世界大战中获胜
对于(int i=0;i
其他的
MessageBox.Show(“Ni nobene vrstice”);
}
私有void infoToolStripMenuItem\u单击(对象发送方,事件参数e)
{
MessageBox.Show(“njihovih albumov.Izvajalce se potem da razvrstit tudi Pomi zvrsti s pomočjo postavke v menuju”节目中的je namenjen vnašanju najljubših glasbenikov”+
“+”V polja vnesemoželjene vrednosti,dodamo vnos katerega lahko urejamo,brišemo itd.Pri datumu mora letnica biti manjša od 2013年”);
}
私有无效sortBygenreToolStripMenuItem\u单击(对象发送方,事件参数e)
{
}
}
}有以下输出:

以下是要计算的代码:

            int CountRock = 0;
            int CountPop = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow)  //last row is new row containing null data, so we won't check this one
                {
                    if (row.Cells[1].Value.ToString() == "Rock")    //cell[1] defines that the index of column is '1'
                        CountRock++;
                    else if (row.Cells[1].Value.ToString() == "Pop")
                        CountPop++;
                }
            }

            MessageBox.Show(CountRock + " , " + CountPop);//Displaying the output
您可以通过仅检查起始字符简化
if语句

if (row.Cells[1].Value.ToString().StartsWith("R"))
同样,您也可以通过使用for或foreach循环编写自己的排序逻辑。

可以吗