我正在做一个项目,我需要在单击阻止按钮后按优先级降序排序我的数据网格。单击块后,该行中的优先级将从数字的任何值更改为-1,这应将其移动到网格的底部并将最高优先级值推送到网格的顶部。它在我第一次点击阻止按钮时工作,但之后没有其他任何响应按钮Ready或Block的任何点击。我想用-1值将所有块项移动到列表底部,然后在单击准备好后将所有项返回到网格中的正确排名。有人可以解释为什么它只在第一次点击删除按钮时对我的网格进行排序吗?
namespace Dispatcher_2._0
{
public partial class Form1 : Form
{
DataTable Table = new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Table.Columns.Add("PID", typeof(int));
Table.Columns.Add("Status", typeof(string));
Table.Columns.Add("Priority", typeof(int));
Table.Rows.Add(91, "Current Process", 0);
Table.Rows.Add(92, "Ready", 23);
Table.Rows.Add(95, "Ready", 22);
Table.Rows.Add(93, "Ready", 28);
Table.Rows.Add(94, "Ready", 44);
dataGridView1.DataSource = Table;
DataGridViewButtonColumn Btn = new DataGridViewButtonColumn();
DataGridViewButtonColumn Btn1 = new DataGridViewButtonColumn();
DataGridViewButtonColumn Btn2 = new DataGridViewButtonColumn();
Btn.Name = "Ready";
Btn.Text = "Ready";
Btn1.Name = "Block";
Btn1.Text = "Block";
Btn2.Name = "Terminate";
Btn2.Text = "Terminate";
Btn.UseColumnTextForButtonValue = true;
Btn1.UseColumnTextForButtonValue = true;
Btn2.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Add(Btn);
dataGridView1.Columns.Add(Btn1);
dataGridView1.Columns.Add(Btn2);
}
private void button3_Click(object sender, EventArgs e)
{
Table.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0)
{
Table.Rows.Add(91, "Current Process", 0);
Table.Rows.Add(92, "Ready", 23);
Table.Rows.Add(95, "Ready", 22);
Table.Rows.Add(93, "Ready", 28);
Table.Rows.Add(94, "Ready", 44);
dataGridView1.DataSource = Table;
}
else { }
dataGridView1.Refresh();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int selectedRow = dataGridView1.CurrentCell.RowIndex;
string value = dataGridView1.Rows[0].Cells[1].FormattedValue.ToString();
string value1 = dataGridView1.Rows[selectedRow].Cells[selectedRow].FormattedValue.ToString();
if (dataGridView1.Columns[e.ColumnIndex].Name == "Ready")
{
dataGridView1.Rows[selectedRow].Cells[1].Value = "Ready";
}
if (dataGridView1.Columns[e.ColumnIndex].Name == "Block")
{
dataGridView1.Rows[selectedRow].Cells[1].Value = "Blocked";
dataGridView1.Rows[selectedRow].Cells[2].Value = "-1";
DataView Tab = new DataView(Table);
Tab.Sort = "Priority desc";
dataGridView1.DataSource = Tab;
}
if (dataGridView1.Columns[e.ColumnIndex].Name == "Terminate")
{
dataGridView1.Rows.RemoveAt(0);
}
}
private void button1_Click(object sender, EventArgs e)
{
Table.Rows.Add(new Random().Next(50, 100), "Ready", textBox1.Text);
}
}
}
答案 0 :(得分:0)
我总是在不使用数据源的情况下填写表格,所以我不确定这是如何工作的。但是,我认为无论如何我都会看到你的问题 - 听起来你需要编写一个自定义比较函数来对表进行排序。只要您拥有必须根据多个列进行排序的数据,就必须这样做。