所以我正在做一个学校项目并且我已接近完成,但我遇到了问题。我的计划"是为了音乐,我有不同的流派。我的任务是以某种方式按照音乐对艺术家进行排序并将其全部计算在内。我的意思是,当你点击"按类型排序"在menustrip中,一个消息框会显示说'摇滚4,流行音乐3等等#34;数字指的是艺术家,有多少是在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)
{
}
}
}
答案 0 :(得分:1)
有输出:
以下是要计算的代码:
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
您可以通过仅检查启动char:
来简化if-statements
if (row.Cells[1].Value.ToString().StartsWith("R"))
以同样的方式,您只需使用for
或foreach
循环即可编写自己的排序逻辑。