簡體   English   中英

如何在datagridview中添加符號

[英]how to add a symbol into datagridview

我正在嘗試實現加載和去sic匯編程序,我使用了3個gridviews,一個存儲讀取的pgm,menoic值和其他一個空的gridview,它將充當symbole表我在嘗試輸入應該持有symbole的新值gridview時遇到問題和它的地址,這是我的代碼。

 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加載和去裝配。 我的代碼如下`命名空間WindowsFormsApplication1 {public partial class Form2:Form {string opcode; string labelz; 字符串操作數; 串m; string fg; 字符串d; int a = 0; 字符串值; static System.Data.OleDb.OleDbConnection con1 = new System.Data.OleDb.OleDbConnection(“Provider = Microsoft.Jet.OleDb.4.0; Data Source = D:/ASSEMBLER.mdb”); public Form2(){InitializeComponent(); GRID1(); GRID2(); 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);

        }


    }## 
  1. 標題

     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(); } 

    }}

    `

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM