使用绑定源设置高级数据网格视图

yhxst69z  于 2021-08-13  发布在  Java
关注(0)|答案(0)|浏览(155)

我决定启动一个新线程,与此线程相关:需要使用绑定源设置datagridview datasource
我非常接近,我认为我的问题与清除数据源、数据读取器、数据适配器等有关。,
我的项目运行查询,为需要运行的下一个查询提供参数。在本例中,在我运行分析查询之后,第一个advanceddatagridview将按预期填充,请参见图1。:图1
单击要使用的分析并将其添加到列表框中。下一个查询应该获取分析信息并查询相关组件,参见图2。图2
最终的结果是,advanceddatagridview现在显示了第二个advanceddatagridview的功能。
我已经裁剪了一些代码,希望我已经提供了足够的代码来帮助您找出为什么我的第一个advanceddatagridview会被更改。在1和2之后,我需要从#2中选择什么,然后运行一个分析查询,它将返回我们需要的结果。这也是像这样处理的10个系列中的第一个。在进一步研究之前,我非常想弄清楚这一点。

using System;
using System.Data;
using System.Windows.Forms;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
using ADGV;

namespace Stored_Query_2
{    
  public partial class frmMain : Form
  {       
    private static string connString = "User Id=;Password=;" +
           "Data Source=:1521/;Pooling=false;";

    private OracleDataAdapter da;
    BindingSource bs = new BindingSource();
    DataSet ds = null;
    private string sql;
    private static string name; 
    private AdvancedDataGridView DGV = new AdvancedDataGridView(); 
    DataTable dt = new DataTable();

    public frmMain()
    {
        InitializeComponent();
    }

    private OracleConnection conn = new OracleConnection();
    private CommandType CommandType;
    private AdvancedDataGridView dgAnalysis;   
    private OracleDataReader dr;

    private void componentsByAnalysisToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //select correct tab
        var caseSwitch = (sender as ToolStripMenuItem).Text;
        switch (caseSwitch)
        {
            case "Components By Analysis":
                //switch to tab
                this.tab1.SelectedTab = this.tabpg1;
                break;
            default:
                //nothing    
                break;
        }
    }                
    private void dgAnalysis_FilterStringChanged(object sender, EventArgs e)
    {
        this.bs.Filter = this.dgAnalysis.FilterString;
    }
    private void dgAnalysis_SortStringChanged(object sender, EventArgs e)
    {
        this.bs.Sort = this.dgAnalysis.SortString;
    }        
    private void dgAnalysis_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            if (dgAnalysis.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
            {
                //Add to listbox
                lstAnalysis.Items.Add(dgAnalysis.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
            }
        }
        catch
        {
            return;
        }
    }
    private void cmdDeleteFromList_Click(object sender, EventArgs e)
    {
        while (lstAnalysis.SelectedItems.Count > 0)
        {
            lstAnalysis.Items.Remove(lstAnalysis.SelectedItems[0]);
        }
    }
    private void cmd2tb1pg1_Click(object sender, EventArgs e)
    {
        string ana = "";
        foreach (string item in lstAnalysis.Items)
        {
            ana += "'" + item.ToString() + "',";
        }
        //Clean up list itens
        ana = ana.Remove(ana.Length - 1, 1);
        sql = "";
        sql = sql + "SELECT DISTINCT LWPROD.COMPONENT.NAME AS Component_Name,";
        sql = sql + " LWPROD.ANALYSIS.NAME AS Analysis_Name";
        sql = sql + " FROM LWPROD.COMPONENT";
        sql = sql + " INNER JOIN LWPROD.ANALYSIS ON ANALYSIS.NAME = COMPONENT.ANALYSIS";
        sql = sql + " WHERE ANALYSIS.NAME IN (" +  ana + ")";
        sql = sql + " ORDER BY ANALYSIS.NAME,COMPONENT.NAME";
        DGV.Name = "dgComponents"; 
        LoadData(sql, dgComponents,"dgComponents");          
        dgComponents.ClearSelection();
    }
    private void cmdtb1pg1_Click(object sender, EventArgs e)
    {
        DGV.Name = "dgAnalysis";
        sql = "SELECT DISTINCT NAME FROM LWPROD.ANALYSIS ORDER BY 1";
        LoadData(sql, dgAnalysis, "dgAnalysis");
        dgAnalysis.ClearSelection();
    }
    private void LoadData(string sql, AdvancedDataGridView DGV, string name)
    {
        try
        {
            string oradb = "User Id=LWPROD_USER_RO;Password=SB17#adlims4prd;" +
        "Data Source=gojoracb-scan.gojo.net:1521/csprod_primary;Pooling=false;";
            OracleConnection conn = new OracleConnection(oradb);
            conn.Open();
            OracleCommand cmd = new OracleCommand();
            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;                
            OracleDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            //dt.Clear();
            //bs.Clear();
            dt.Load(dr);
            bs.DataSource = dt;                
        }
        catch
        {
        }
        finally
        {   
            switch (name)
            {
                case "dgAnalysis":                   
                    dgAnalysis.DataSource = bs;
                    dt.Clear();
                    bs.ResetBindings(false);
                    dr =null;
                    conn.Dispose();
                    break;
                case "dgComponents":
                    dgComponents.DataSource = bs;
                    dt.Clear();
                    bs.ResetBindings(false);
                    dr =null;
                    conn.Dispose();
                    break;
            }
        }
    }
  }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题