无法在Windows窗体MVP中实现CRUD操作

时间:2018-12-17 20:44:42

标签: c# winforms crud mvp

我是菜鸟,尝试创建一个至少在某种程度上类似于真实MVP模式的项目。因此,我在通过MVP从DataGridView中的文本框中保存输入的数据以保存然后保存到文件(CRUD或基本操作)的实现方面遇到了问题

这是模特:

 public class Laboratory
{

    public string nazv { get; set; }
    public string mera { get; set; }
    public double kolvo { get; set; }
    public string date { get; set; }
    public string godn { get; set; }
    public string postav { get; set; }
    public string otv { get; set; }
    public string kom { get; set; }
}

这是界面:

public interface ILaboratory  
{
    string text1 { get; set; }
    string text2 { get; set; }
    string text3 { get; set; }
    string text4 { get; set; }
    string text5 { get; set; }
    string text6 { get; set; }
    string text7 { get; set; }
    string text8 { get; set; }

    event EventHandler OnAddInTable; 
    event EventHandler OpenForm; 
    event EventHandler DelStroka;
}

主持人:

public class LaboratoryPr
{

    ILaboratory reactivView;  

    public LaboratoryPr(ILaboratory view) 
    {
        reactivView = view;
        view.OnAddInTable += VOnAddInTable;
        view.OpenForm += VOpenForm;
        //view.DelStroka += VDelStroka;
        //TRY TO DELETE FROM DATAGRIDVIEW
    }
    public void DataGridView1(string query)
    {

    }
    public static void VOnAddInTable(object sender, EventArgs e)
    {

       //TRY TO ADD SOME DATA INTO DATAGRIDVIEW
    }
    public void VOpenForm(object sender, EventArgs e) //открытие формы - 2
    {

        MessageBox.Show("000");
    }


    public void ReactivLab()
    {
        Laboratory reactiv = new Laboratory();
        reactiv.nazv = reactivView.text1;
        reactiv.mera = reactivView.text2;
        reactiv.kolvo = double.Parse(reactivView.text3);
        reactiv.date = reactivView.text4;
        reactiv.godn = reactivView.text5;
        reactiv.postav = reactivView.text6;
        reactiv.otv = reactivView.text7;
        reactiv.kom = reactivView.text8;        
    }
}

和表格

public partial class Form1 : Form, ILaboratory
{
    private readonly string TemplateFileName = @"C:\Users\yaya-\OneDrive\5_semestr\test.docx";

    public  string text1
    {
        get{return textBox1.Text;} 
        set{textBox1.Text = value;}
    }
    public string text2
    {
        get{return comboBox1.Text;}
        set{comboBox1.Text = value;}
    }
    public  string text3
    {
        get{return textBox2.Text;}
        set{textBox2.Text = value;}
    }
    public  string text4
    {
        get{return dateTimePicker1.Text;}
        set{dateTimePicker1.Text = value;}
    }
    public  string text5
    {
        get{return dateTimePicker2.Text;}
        set{dateTimePicker2.Text = value;}
    }
    public  string text6
    {
        get{return textBox3.Text;}
        set{textBox3.Text = value;}
    }
    public  string text7
    {
        get{return comboBox2.Text;}
        set{comboBox2.Text = value;}
    }
    public  string text8
    {
        get{return richTextBox1.Text;}
        set{richTextBox1.Text = value;}
    }

  // public object DataGridView1 { get; private set; }


    public Form1()
    {
        InitializeComponent();
    }

    public event EventHandler OnAddInTable;
    public event EventHandler OpenForm;
    public event EventHandler DelStroka;

    private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }
    public void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
    {
        var propValue = DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
        if (propValue == null) return;

    }

    public void button1_Click(object sender, EventArgs e)
    {
        OnAddInTable(sender, e);
       //dataGridView1.Rows.Add(text1, text2, text3, text4, text5, text6, text7, text8);
    }
    private void button3_Click(object sender, EventArgs e)
    {
        DataGridView1.Rows.Clear();
    }
    public void Tabl(DataGridViewCellEventArgs grid)
    {
    }

    private void button4_Click(object sender, EventArgs e)
    {
        int a = DataGridView1.SelectedCells[0].RowIndex;
        DataGridView1.Rows.RemoveAt(a);
    }

    private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Stream n = null;
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if ((n = openFileDialog1.OpenFile()) != null)
            {
                StreamReader myread = new StreamReader(n);
                string[] str;
                int num = 0;
                try
                {
                    string[] str1 = myread.ReadToEnd().Split('\n');
                    num = str1.Count();
                    DataGridView1.RowCount = num;
                    for (int i = 0; i < num; i++)
                    {
                        str = str1[i].Split('^');
                        for (int j = 0; j < DataGridView1.ColumnCount; j++)
                            try
                            {
                                string data = str[j].Replace("[simvol]", "^");
                                DataGridView1.Rows[i].Cells[j].Value = str[j];
                            }
                            catch { }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally {
                    myread.Close();
                }
            }
        }
    }

    private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Stream myStream;
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if ((myStream = saveFileDialog1.OpenFile()) != null)
            {
                StreamWriter myWritet = new StreamWriter(myStream);
                try
                {
                    for (int i = 0; i < DataGridView1.RowCount - 1; i++)
                    {
                        for (int j = 0; j < DataGridView1.ColumnCount; j++)
                        {
                            string data = DataGridView1.Rows[i].Cells[j].Value.ToString().Replace("^", "[simvol]");
                            myWritet.Write(data + '^');
                        }
                        myWritet.WriteLine();
                    }
                }    
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    myWritet.Close();
                }
                myStream.Close();
                }
            }
        }

    private void label7_Click(object sender, EventArgs e)
    {

    }

    private void label8_Click(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        DataGridView1.TopLeftHeaderCell.Value = "№";

    }

    private void label9_Click(object sender, EventArgs e)
    {

    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        int a = DataGridView1.SelectedCells[0].RowIndex;
        DataGridView1.Rows.RemoveAt(a);
    }


    private void aboutProgrammToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenForm(sender, e);
    }

    private void label10_Click(object sender, EventArgs e)
    {

    }

    private void button5_Click(object sender, EventArgs e) //export word
    {
        var wordApp = new word.Application();
        wordApp.Visible = false;
        var wordDocument = wordApp.Documents.Open(TemplateFileName);
    }



    void SaveTable(DataGridView dataGridView1) 
    {
        string patch = System.IO.Directory.GetCurrentDirectory() + @"\" + "Save_Chanel.xlsx";
        Excel.Application excelapp = new Excel.Application();
        Excel.Workbook workbook = excelapp.Workbooks.Add();
        Excel.Worksheet worksheet = workbook.ActiveSheet;

        for (int i = 1; i < dataGridView1.RowCount +1; i++)
        {
            for (int j = 1; j<dataGridView1.ColumnCount +1; j++)
            {
                worksheet.Rows[i].Columns[j] = dataGridView1.Rows[i - 1].Cells[j - 1].Value;             
            }
        }
        excelapp.AlertBeforeOverwriting = false;
        workbook.SaveAs(patch);
        excelapp.Quit();
        MessageBox.Show("Success saved!", "Export excel", MessageBoxButtons.OK);
    }
    private void button2_Click_1(object sender, EventArgs e)
    {

    }

    private void button6_Click(object sender, EventArgs e)
    {
        SaveTable(DataGridView1);
    }
}

}

0 个答案:

没有答案