我有一个问题,因为我在C#中编写了一个关于fcfs算法的程序,我有一个问题,如何从datagridview导入算法,然后在我的datagridview2上显示这里是我的代码:
using System;
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;
namespace Projekt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable table = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
table.Columns.Add("Process", typeof(int));
table.Columns.Add("Arrival Time", typeof(string));
table.Columns.Add("Burst Time", typeof(string));
table.Columns.Add("Piority", typeof(int));
table.Rows.Add("1");
table.Rows.Add("2");
table.Rows.Add("3");
table.Rows.Add("4");
dataGridView1.DataSource = table;
}
private void button1_Click(object sender, EventArgs e)
{
Random rand = new Random();
dataGridView1[1, 0].Value = rand.Next(5, 30);
dataGridView1[2, 0].Value = rand.Next(5, 30);
dataGridView1[3, 0].Value = rand.Next(5, 30);
dataGridView1[1, 1].Value = rand.Next(5, 30);
dataGridView1[2, 1].Value = rand.Next(5, 30);
dataGridView1[3, 1].Value = rand.Next(5, 30);
dataGridView1[1, 2].Value = rand.Next(5, 30);
dataGridView1[2, 2].Value = rand.Next(5, 30);
dataGridView1[3, 2].Value = rand.Next(5, 30);
dataGridView1[1, 3].Value = rand.Next(5, 30);
dataGridView1[2, 3].Value = rand.Next(5, 30);
dataGridView1[3, 3].Value = rand.Next(5, 30);
}
private void FCFS_Click(object sender, EventArgs e)
{
}
}
}
你可以帮我解决这个问题
答案 0 :(得分:0)
请尝试以下代码。第0列和第0列3是int,而第1列和第1列是2个字符串。您不能将int放入字符串列
using System;
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;
namespace Projekt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable table = new DataTable();
DataTable table2 = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
table.Columns.Add("Process", typeof(int));
table.Columns.Add("Arrival Time", typeof(string));
table.Columns.Add("Burst Time", typeof(string));
table.Columns.Add("Piority", typeof(int));
table.Rows.Add("1");
table.Rows.Add("2");
table.Rows.Add("3");
table.Rows.Add("4");
dataGridView1.DataSource = table;
}
private void button1_Click(object sender, EventArgs e)
{
Random rand = new Random();
table.Rows[0][0] = rand.Next(5, 30);
table.Rows[1][0] = rand.Next(5, 30);
table.Rows[2][0] = rand.Next(5, 30);
table.Rows[3][0] = rand.Next(5, 30);
table.Rows[0][3] = rand.Next(5, 30);
table.Rows[1][3] = rand.Next(5, 30);
table.Rows[2][3] = rand.Next(5, 30);
table.Rows[3][3] = rand.Next(5, 30);
dataGridView1 = null; //to force update
dataGridView1.DataSource = table;
}
private void FCFS_Click(object sender, EventArgs e)
{
table2 = table.Clone();
dataGridView2.DataSource = table2;
}
}
}
答案 1 :(得分:0)
这两行不起作用
dataGridView1 = null; //强制更新 dataGridView1.DataSource = table;
他们只克隆列而不是值为
的行