如何在datagridview中添加符号
本文关键字:添加 符号 datagridview | 更新日期: 2024-10-20 21:38:25
我正在尝试实现加载并执行sic汇编程序,我使用了三个网格视图,一个存储读取的pgm、menoic值,另一个存储空网格视图,它将充当符号表。我在尝试输入新的值网格视图时遇到了问题,该视图应包含符号及其地址,这是我的代码。
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
static System.Data.OleDb.OleDbConnection con1 = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=D:/ASSEMBLER.mdb");
string start_address;
int k = 0;
string value;
int ma;
int nxt_add = 0;
string opcode;
public Form2()
{
InitializeComponent();
grid1();
grid2();
grid3();
}
private void grid1()
{
// var ss = new List<string> { "BYTE", "RESW", "RESB", "WORD" };
try
{
// InitializeComponent();
DataSet ds = new DataSet();
System.Data.OleDb.OleDbDataAdapter adapReport = new System.Data.OleDb.OleDbDataAdapter("select Adress as [address], opcode as [Symbol],operand as [opco],op as [operand],ooo as[obj] from read_pgm" + "'", con1);
adapReport.Fill(ds, "read_pgm");
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "read_pgm";
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.HeaderCell.Style.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel);
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
dataGridView1.Rows[i].Cells[0].Style.Font = new Font("Arial", 9, FontStyle.Bold);
dataGridView1.Rows[i].Cells[1].Style.Font = new Font("Arial", 9, FontStyle.Bold);
dataGridView1.Rows[i].Cells[2].Style.Font = new Font("Arial", 9, FontStyle.Bold);
dataGridView1.Rows[i].Cells[3].Style.Font = new Font("Arial", 9, FontStyle.Bold);
}
Int16 b = Convert.ToInt16(dataGridView1.Rows[1].Cells[0].Value);
//start_address = Convert.ToString(dataGridView1.Rows[1].Cells[0].Value);
// MessageBox.Show(add + " in second form");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
con1.Close();
}
}
private void grid2()
{
con1.Open();
try
{
//InitializeComponent();
DataSet ds1 = new DataSet();
System.Data.OleDb.OleDbDataAdapter adapReport = new System.Data.OleDb.OleDbDataAdapter("select SYMBOLE as [Symbol],ADDRESS as [ADDRESS] from SYMBOL_TABLE" + "'", con1);
adapReport.Fill(ds1, "SYMBOL_TABLE");
dataGridView2.AutoGenerateColumns = true;
dataGridView2.DataSource = ds1;
dataGridView2.DataMember = "SYMBOL_TABLE";
foreach (DataGridViewColumn col in dataGridView2.Columns)
{
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.HeaderCell.Style.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel);
}
for (int i = 0; i < dataGridView2.Rows.Count - 1; i++)
{
dataGridView2.Rows[i].Cells[0].Style.Font = new Font("Arial", 9, FontStyle.Bold);
dataGridView2.Rows[i].Cells[1].Style.Font = new Font("Arial", 9, FontStyle.Bold);
}
//Int16 b = Convert.ToInt16(dataGridView1.Rows[1].Cells[0].Value);
//start_address = Convert.ToString(dataGridView1.Rows[1].Cells[0].Value);
// MessageBox.Show(add + " in second form");
con1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void grid3()
{
try
{
con1.Open();
// InitializeComponent();
DataSet ds2 = new DataSet();
System.Data.OleDb.OleDbDataAdapter adapReport = new System.Data.OleDb.OleDbDataAdapter("select opcode as [opcode],val as [value] from Mnemoni" + "'", con1);
adapReport.Fill(ds2, "Mnemoni");
dataGridView3.AutoGenerateColumns = true;
dataGridView3.DataSource = ds2;
dataGridView3.DataMember = "Mnemoni";
foreach (DataGridViewColumn col in dataGridView3.Columns)
{
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.HeaderCell.Style.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel);
}
for (int i = 0; i < dataGridView3.Rows.Count - 1; i++)
{
dataGridView3.Rows[i].Cells[0].Style.Font = new Font("Arial", 9, FontStyle.Bold);
dataGridView3.Rows[i].Cells[1].Style.Font = new Font("Arial", 9, FontStyle.Bold);
}
//Int16 b = Convert.ToInt16(dataGridView1.Rows[1].Cells[0].Value);
//start_address = Convert.ToString(dataGridView1.Rows[1].Cells[0].Value);
// MessageBox.Show(add + " in second form");
con1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form2_Load(object sender, EventArgs e)
{
cal(dataGridView1, dataGridView2, dataGridView3);
}
private void add()
{
try
{
con1.Open();
System.Data.OleDb.OleDbCommand top = new System.Data.OleDb.OleDbCommand(
"INSERT INTO SYMBOL_TABLE (" +
"SYMBOLE,ADDRESS" +
") VALUES (?,?)", con1);
top.Parameters.AddWithValue("?", opcode);
top.Parameters.AddWithValue("?", "xxxx");
top.ExecuteNonQuery();
con1.Close();
grid2();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void cal(DataGridView dataGridView1, DataGridView dataGridView2, DataGridView dataGridView3)
{
start_address = Convert.ToString(dataGridView1.Rows[1].Cells[0].Value);
MessageBox.Show("this is start adder" + start_address);
for (int i = 1; i < dataGridView1.Rows.Count; i++)
{
opcode = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
string operand = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
string op = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
MessageBox.Show(opcode);
string cv = dataGridView1.Rows[i].Cells[0].Value.ToString();
MessageBox.Show(cv);
// int nxt_add =Convert.ToInt16(cv);
nxt_add = nxt_add + 3;
string address = nxt_add.ToString("X");
dataGridView1.Rows[i + 1].Cells[0].Value = address;
// MessageBox.Show(operand);
//MessageBox.Show(op);
if (opcode != null && opcode != "")
{
MessageBox.Show("inside opcode loop");
for (int j = 0; j < dataGridView2.Rows.Count; j++)
{
//MessageBox.Show(dataGridView2.Rows[j].Cells[0].Value.ToString());
try
{
if (dataGridView2.Rows[j].Cells[0].Value.ToString() == opcode)
{
ma = j;
string m = dataGridView1.Rows[j].Cells[0].Value.ToString();
MessageBox.Show("value of m" + m);
dataGridView2.Rows[j].Cells[1].Value = m;
MessageBox.Show("if condition satisified block of opcode and it is exiting now ");
break;
}
}
catch (NullReferenceException e)
{
MessageBox.Show("opcode value is " + opcode);
int a = dataGridView2.NewRowIndex;
dataGridView2.Rows[a].Cells[0].Value = opcode;
dataGridView2.Rows[a].Cells[1].Value = "xxxx";
// ma = k;
// k++;
MessageBox.Show("exiting catch block of opcode");
break;
}
}
}
for (int y = 0; y < dataGridView2.Rows.Count; y++)
{
MessageBox.Show("content of symbole table");
MessageBox.Show(dataGridView2.Rows[y].Cells[0].Value.ToString());
MessageBox.Show(dataGridView2.Rows[y].Cells[1].Value.ToString());
}
if (operand != null)
{
MessageBox.Show("inside the operan loop");
for (int kj = 0; kj < dataGridView3.Rows.Count; kj++)
{
try
{
if (dataGridView3.Rows[kj].Cells[0].Value.ToString() == operand)
{
value = dataGridView3.Rows[kj].Cells[1].Value.ToString();
MessageBox.Show(value);
break;
}
else
{
// MessageBox.Show("invalid operand");
continue;
}
}
catch (Exception e)
{
MessageBox.Show("inside the catch block of operand loop ");
// MessageBox.Show(e.Message+e.InnerException);
}
}
}
if (op != null)
{
MessageBox.Show("in the op loop");
if (op == "")
{
MessageBox.Show("op is empty space ****");
}
int[] num = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
if (op.Contains(Convert.ToString(num)))
{
}
int f = dataGridView2.CurrentCellAddress.X;
for (int j = 0; j < dataGridView2.Rows.Count; j++)
{
//MessageBox.Show(dataGridView2.Rows[j].Cells[0].Value.ToString());
try
{
if (dataGridView2.Rows[j].Cells[0].Value.ToString() == opcode)
{
ma = j;
string m = dataGridView1.Rows[j].Cells[0].Value.ToString();
MessageBox.Show("value of m" + m);
dataGridView2.Rows[j].Cells[1].Value = m;
MessageBox.Show("if condition satisified block of opcode and it is exiting now ");
break;
}
}
catch (NullReferenceException e)
{
MessageBox.Show("opcode value is " + opcode);
int a = dataGridView2.NewRowIndex;
dataGridView2.Rows[a].Cells[0].Value = opcode;
dataGridView2.Rows[a].Cells[1].Value = "xxxx";
// ma = k;
// k++;
MessageBox.Show("exiting catch block of opcode");
break;
}
MessageBox.Show("op value is " + op);
try
{
int b = dataGridView2.NewRowIndex;
dataGridView2.Rows[b].Cells[0].Value = op;
dataGridView2.Rows[b].Cells[1].Value = "xxxx";
}
catch (Exception v)
{
MessageBox.Show(v.Message);
}
// dataGridView2.BeginEdit(true);
// dataGridView2.EndEdit();
MessageBox.Show("exiting op loop");
}
MessageBox.Show("value of i is " + i);
for (int o = 0; o < dataGridView2.Rows.Count; o++)
{
MessageBox.Show(dataGridView2.Rows[o].Cells[0].Value.ToString());
MessageBox.Show(dataGridView2.Rows[o].Cells[1].Value.ToString());
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
我已经找到了解决问题的方法,只需进行一些修改,我已经完成了简单的sic加载和go汇编程序。我的代码如下`命名空间WindowsFormsApplication1{公共分部类Form2:Form{字符串操作码;字符串标签;字符串操作数;字符串m;字符串fg;字符串d;int a=0;字符串值;static System.Data.OleDb.OleDbConnection con1=新System.Data.OleDb.OleDbConnection("提供程序=Microsoft.Jet.OleDb.4.0;数据源=D:/ASSEMBLER.mdb");公共窗体2(){InitializeComponent();grid1();网格2();grid3();
}
private void grid1()
{
try
{
DataSet ds = new DataSet();
System.Data.OleDb.OleDbDataAdapter adapReport = new System.Data.OleDb.OleDbDataAdapter("select Adress as [address], opcode as [Symbol],operand as [opco],op as [operand],ooo as[obj] from read_pgm" + "'", con1);
adapReport.Fill(ds, "read_pgm");
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "read_pgm";
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.HeaderCell.Style.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel);
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
dataGridView1.Rows[i].Cells[0].Style.Font = new Font("Arial", 9, FontStyle.Bold);
dataGridView1.Rows[i].Cells[1].Style.Font = new Font("Arial", 9, FontStyle.Bold);
dataGridView1.Rows[i].Cells[2].Style.Font = new Font("Arial", 9, FontStyle.Bold);
dataGridView1.Rows[i].Cells[3].Style.Font = new Font("Arial", 9, FontStyle.Bold);
}
Int16 b = Convert.ToInt16(dataGridView1.Rows[1].Cells[0].Value);
//start_address = Convert.ToString(dataGridView1.Rows[1].Cells[0].Value);
// MessageBox.Show(add + " in second form");
con1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void grid2()
{
con1.Open();
try
{
DataSet ds1 = new DataSet();
System.Data.OleDb.OleDbDataAdapter adapReport = new System.Data.OleDb.OleDbDataAdapter("select SYMBOLE as [Symbol],ADDRESS as [ADDRESS] from SYMBOL_TABLE" + "'", con1);
adapReport.Fill(ds1, "SYMBOL_TABLE");
dataGridView2.AutoGenerateColumns = true;
dataGridView2.DataSource = ds1;
dataGridView2.DataMember = "SYMBOL_TABLE";
foreach (DataGridViewColumn col in dataGridView2.Columns)
{
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.HeaderCell.Style.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel);
}
for (int i = 0; i < dataGridView2.Rows.Count - 1; i++)
{
dataGridView2.Rows[i].Cells[0].Style.Font = new Font("Arial", 9, FontStyle.Bold);
dataGridView2.Rows[i].Cells[1].Style.Font = new Font("Arial", 9, FontStyle.Bold);
}
//Int16 b = Convert.ToInt16(dataGridView1.Rows[1].Cells[0].Value);
//start_address = Convert.ToString(dataGridView1.Rows[1].Cells[0].Value);
// MessageBox.Show(add + " in second form");
con1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//con1.Close();
}
private void grid3()
{
con1.Open();
try
{
DataSet ds2 = new DataSet();
System.Data.OleDb.OleDbDataAdapter adapReport = new System.Data.OleDb.OleDbDataAdapter("select opcode as [opcode],val as [value] from Mnemoni" + "'", con1);
adapReport.Fill(ds2, "Mnemoni");
dataGridView3.AutoGenerateColumns = true;
dataGridView3.DataSource = ds2;
dataGridView3.DataMember = "Mnemoni";
foreach (DataGridViewColumn col in dataGridView3.Columns)
{
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.HeaderCell.Style.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel);
}
for (int i = 0; i < dataGridView3.Rows.Count - 1; i++)
{
dataGridView3.Rows[i].Cells[0].Style.Font = new Font("Arial", 9, FontStyle.Bold);
dataGridView3.Rows[i].Cells[1].Style.Font = new Font("Arial", 9, FontStyle.Bold);
}
//Int16 b = Convert.ToInt16(dataGridView1.Rows[1].Cells[0].Value);
//start_address = Convert.ToString(dataGridView1.Rows[1].Cells[0].Value);
// MessageBox.Show(add + " in second form");
con1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}##
航向
#
private void cal(DataGridView dataGridView1, DataGridView dataGridView2, DataGridView dataGridView3) { string start_address = Convert.ToString(dataGridView1.Rows[1].Cells[0].Value); /// MessageBox.Show("this is start adder" + start_address); for (int i = 1; i < dataGridView1.Rows.Count - 2; i++) { labelz = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value); opcode = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value); operand = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value); string objcod = Convert.ToString(dataGridView1.Rows[i].Cells[4].Value); a = a + Convert.ToInt16(3); // start_address = Convert.ToString(a); string s = a.ToString("X"); string bv = a.ToString(); if (dataGridView1.Rows[i].Cells[2].Value.ToString() != "END") { if (bv.Length == 2) { string tr = start_address.Remove(1, 2); dataGridView1.Rows[i + 1].Cells[0].Value = tr + s; } else if (bv.Length < 2) { string tr = start_address.Remove(2, 1); dataGridView1.Rows[i + 1].Cells[0].Value = tr + s; } } d = dataGridView1.Rows[i].Cells[0].Value.ToString(); if (labelz != null && labelz != "") { MessageBox.Show("label is" + labelz + " value is" + d); im(labelz, d); grid2(); } if (opcode != null) { for (int kj = 0; kj < dataGridView3.Rows.Count; kj++) { try { if (dataGridView3.Rows[kj].Cells[0].Value.ToString() == opcode) { dataGridView1.Rows[i].Cells[4].Value = dataGridView3.Rows[kj].Cells[1].Value.ToString(); value = dataGridView3.Rows[kj].Cells[1].Value.ToString(); break; } } catch (Exception e) { string qw = dataGridView1.Rows[i].Cells[3].Value.ToString(); if (qw.Length == 4) { value = "00" + qw; dataGridView1.Rows[i].Cells[4].Value = value; } if (qw.Length == 1) { value = "00000" + qw; dataGridView1.Rows[i].Cells[4].Value = value; } if (qw.Length == 2) { value = "0000" + qw; dataGridView1.Rows[i].Cells[4].Value = value; } if (qw.Length == 3) { value = "000" + qw; dataGridView1.Rows[i].Cells[4].Value = value; } } } if (operand != ""&& opcode != "WORD" | opcode != "RESW") { try { for (int jm = 0; jm < dataGridView1.Rows.Count - 1; jm++) { //break; if (dataGridView2.Rows[jm].Cells[0].Value.ToString() == operand) { for (int hj = 0; hj < dataGridView1.Rows.Count; hj++) { if (dataGridView1.Rows[hj].Cells[1].Value.ToString() == operand) { m = dataGridView1.Rows[hj].Cells[0].Value.ToString(); MessageBox.Show("symbole" + operand + " is already in symbol table" + "with address" + m); cv(operand,m); grid2(); break; } /* else { cv(operand,"XXXX"); grid2(); }*/ } } } } catch (NullReferenceException e) { MessageBox.Show("symbol" + operand + "is not present in symbol table & entered with value" + "xxxx"); cv(operand, "XXXX"); grid2(); //break; } } if (operand == "") { dataGridView1.Rows[i].Cells[4].Value = value + "0000"; } } MessageBox.Show("object code is" + dataGridView1.Rows[i].Cells[4].Value.ToString()); if (dataGridView1.Rows[i].Cells[4].Value.ToString() == "") { for (int jh = 0; jh < dataGridView2.Rows.Count; jh++) { try { if (dataGridView2.Rows[jh].Cells[0].Value.ToString() == operand) { fg = dataGridView2.Rows[jh].Cells[1].Value.ToString(); dataGridView1.Rows[i].Cells[4].Value = value + fg; MessageBox.Show(operand); break; } } catch (NullReferenceException v) { break; } } } for (int mi = 1; mi < dataGridView1.Rows.Count - 2; mi++) { if (dataGridView1.Rows[mi].Cells[0].Value.ToString().Length == 3) { string sd = dataGridView1.Rows[mi].Cells[0].Value.ToString(); sd = sd.Insert(1, "0"); dataGridView1.Rows[mi].Cells[0].Value = sd; } try { string kj = dataGridView1.Rows[mi].Cells[4].Value.ToString(); // MessageBox.Show("kj length is" + kj.Length); if (kj.Length < 6) { for (int re = 0; re < dataGridView2.Rows.Count; re++) { if (dataGridView2.Rows[re].Cells[0].Value.ToString() == dataGridView1.Rows[mi].Cells[3].Value.ToString()) { string vv = dataGridView2.Rows[re].Cells[1].Value.ToString(); dataGridView1.Rows[mi].Cells[4].Value = kj + vv; } } } } catch (NullReferenceException v) { } } } } private static void im(string lab, string d) { System.Data.OleDb.OleDbConnection con1 = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=D:/ASSEMBLER.mdb"); try { con1.Open(); System.Data.OleDb.OleDbCommand top = new System.Data.OleDb.OleDbCommand( "INSERT INTO SYMBOL_TABLE (" + "SYMBOLE,ADDRESS" + ") VALUES (?,?)", con1); top.Parameters.AddWithValue("?", lab); top.Parameters.AddWithValue("?", d); // top.Parameters.AddWithValue("?", mcs[2]); top.ExecuteNonQuery(); con1.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } public static void cv(string a, string b) { try { con1.Open(); System.Data.OleDb.OleDbCommand top1 = new System.Data.OleDb.OleDbCommand("UPDATE SYMBOL_TABLE SET[ADDRESS] = ? where SYMBOLE = ?", con1); top1.Parameters.AddWithValue("?", a); top1.Parameters.AddWithValue("?", b); top1.ExecuteNonQuery(); con1.Close(); } catch (Exception e) { MessageBox.Show(e.Message); } } private void Form2_Load(object sender, EventArgs e) { cal(dataGridView1, dataGridView2, dataGridView3); } private void button1_Click(object sender, EventArgs e) { this.Close(); }
}}
`