避免按钮点击/控制datagridview;自动显示

本文关键字:datagridview 显示 控制 按钮 | 更新日期: 2023-09-27 18:08:39

我不想使用按钮控件,我只想显示数据,然后在gridview中传输到excel作为代码状态。是否有办法消除/跳过按钮控制/点击?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        string[] fileEntries = Directory.GetFiles(@"c:'Sciclone UAC", "*.cfg*");
        foreach (string fileName in fileEntries)
        {
            var query = from file in fileEntries
                        let doc = XDocument.Load(file)
                        let x = doc.Descendants("XAxisCalib").Single()
                        let y = doc.Descendants("YAxisCalib").Single()
                        let z = doc.Descendants("ZAxisCalib").Single()
                        select new
                        {
                            XMax = x.Element("Max").Value,
                            XMin = x.Element("Min").Value,
                            YMax = y.Element("Max").Value,
                            YMin = y.Element("Min").Value,
                            ZMax = z.Element("Max").Value,
                            ZMin = z.Element("Min").Value
                        }; 

            var bs3 = new BindingSource { DataSource = query };
            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.AutoSize = true;
            dataGridView1.DataSource = bs3;
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        Excel.Application xlApp ;
        Excel.Workbook xlWorkBook ;
        Excel.Worksheet xlWorkSheet ;
        object misValue = System.Reflection.Missing.Value;
        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

避免按钮点击/控制datagridview;自动显示

把代码拉到函数中:

public Form1()
{
    InitializeComponent();
    // Could add calls here
    LoadData();
    RunExcel();
}
// Remove this button click handler if desired
private void button1_Click(object sender, EventArgs e)
{
   LoadData();
}
// Remove this button click handler if desired
private void button2_Click(object sender, EventArgs e)
{
   RunExcel();
}
private void LoadData()
{
    string[] fileEntries = Directory.GetFiles(@"c:'Sciclone UAC", "*.cfg*");
    foreach (string fileName in fileEntries)
    {
        var query = from file in fileEntries
                    let doc = XDocument.Load(file)
                    let x = doc.Descendants("XAxisCalib").Single()
                    let y = doc.Descendants("YAxisCalib").Single()
                    let z = doc.Descendants("ZAxisCalib").Single()
                    select new
                    {
                        XMax = x.Element("Max").Value,
                        XMin = x.Element("Min").Value,
                        YMax = y.Element("Max").Value,
                        YMin = y.Element("Min").Value,
                        ZMax = z.Element("Max").Value,
                        ZMin = z.Element("Min").Value
                    }; 

        var bs3 = new BindingSource { DataSource = query };
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.AutoSize = true;
        dataGridView1.DataSource = bs3;
    }
}
private void RunExcel()
{
   Excel.Application xlApp ;
   Excel.Workbook xlWorkBook ;
   Excel.Worksheet xlWorkSheet ;
   object misValue = System.Reflection.Missing.Value;
   xlApp = new Excel.Application();
   xlWorkBook = xlApp.Workbooks.Add(misValue);
   xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
   ....
}

您想要删除哪个按钮?要消除按钮点击,只需订阅dataGridView1。DataBindingComplete事件,并将代码移到那里。

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
  // your excel code
}

要消除button1 click,只需将该代码移动到构造函数中…

只需移动您的代码,并在datasourcechanged事件上传输数据:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        // Add the handler for the DataSourceChanged event.
        dataGrid1.DataSourceChanged += new EventHandler(DataGrid1_DataSourceChanged);
        LoadGridData();
    }
    private void LoadGridData()
    {
        string[] fileEntries = Directory.GetFiles(@"c:'Sciclone UAC", "*.cfg*");
        foreach (string fileName in fileEntries)
        {
            var query = from file in fileEntries
                        let doc = XDocument.Load(file)
                        let x = doc.Descendants("XAxisCalib").Single()
                        let y = doc.Descendants("YAxisCalib").Single()
                        let z = doc.Descendants("ZAxisCalib").Single()
                        select new
                        {
                            XMax = x.Element("Max").Value,
                            XMin = x.Element("Min").Value,
                            YMax = y.Element("Max").Value,
                            YMin = y.Element("Min").Value,
                            ZMax = z.Element("Max").Value,
                            ZMin = z.Element("Min").Value
                        }; 

            var bs3 = new BindingSource { DataSource = query };
            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.AutoSize = true;
            dataGridView1.DataSource = bs3;
        }
    }
    private void DataGrid1_DataSourceChanged(object sender, EventArgs e)
    {
        DataGrid thisGrid = (DataGrid) sender;
        Excel.Application xlApp ;
        Excel.Workbook xlWorkBook ;
        Excel.Worksheet xlWorkSheet ;
        object misValue = System.Reflection.Missing.Value;
        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);