需要使用绑定源设置datagridview数据源

bt1cpqcv  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(354)

我使用的是高级datagridview,要使用它附带的过滤器,您需要使用绑定源。我正在编写一个oracle查询(实际上是其中的几个),并将结果用作数据源。我似乎无法使它正常工作。我在google上搜索了所有的解决方案,都试过了,但都没有成功。
我的代码:

public partial class frmMain : Form
{
    private string sql;
    public DataGridView DVG = new DataGridView();
    public BindingSource bs = new BindingSource();     
    private static string connectionString = "User Id=;Password=;" +
            "Data Source=:1521/;Pooling=false;";

    private void cmdtb1pg1_Click(object sender, EventArgs e)
    {
        // Get Analysis
        sql = "SELECT DISTINCT NAME FROM LWPROD.ANALYSIS ORDER BY 1";                
        bs.DataSource = GetData(sql, dgAnalysis);    
        dgAnalysis.ClearSelection();            
    }

    private BindingSource GetData(string sql, DataGridView DGV)
    {
        DataTable table = new DataTable();
        OracleConnection con = new OracleConnection(connectionString);
        BindingSource bs = new BindingSource();
        try
        {                         
            DataSet ds = new DataSet();
            OracleCommand cmd = new OracleCommand();
            con.Open();
            OracleDataAdapter da = new OracleDataAdapter();                
            da.Fill(ds, connectionString);
            bs.DataSource = da;
            return bs;
        }
        catch
        {
            return bs;
        }
        finally
        {
            var name = DGV.Name;
            switch (name)
            {
                case "dgAnalysis":
                    dgAnalysis.DataSource = bs; 
                    break;
                case "dgComponents":
                    dgComponents.DataSource = bs;   
                    break;                        
            }           
        }            
    }
ac1kyiln

ac1kyiln1#

下面是一些更新的代码。这仍然不起作用。在这次尝试中。

1.    ` bs.DataSource = GetData(sql, dgAnalysis);` is stating that cannot implicitly convert type void to object.

2.     ` bs = new BindingSource(ds, DGV); ` is stating that Argument 2 cannot convert from ADGV,AdvancedDataGridView to string. DGV is a variable that contains the name of the datagridview being worked with. See 1. it's working with dgAnalysis, later it's working with dgComponents. I have multiple other datagridview to fill.

private void cmdtb1pg1_Click(object sender, EventArgs e)
    {
        // Get Analysis
        sql = "SELECT DISTINCT NAME FROM LWPROD.ANALYSIS ORDER BY 1";                
        bs.DataSource = GetData(sql, dgAnalysis);    
        dgAnalysis.ClearSelection();            
    }       

    private  void GetData(string sql, AdvancedDataGridView DGV)
    {       
        OracleConnection con = new OracleConnection(connectionString);           
        OracleDataAdapter table = new OracleDataAdapter();
        try
        {
            DataSet ds = new DataSet();
            OracleCommand cmd = new OracleCommand();
            con.Open();
            OracleDataAdapter da = new OracleDataAdapter();
            bs = new BindingSource(ds, DGV);              
        }
        catch
        {                
        }
        finally
        {
            var name = DGV.Name;
            switch (name)
            {
                case "dgAnalysis":
                    dgAnalysis.DataSource = bs; 
                    break;
                case "dgComponents":
                    dgComponents.DataSource = bs;   
                    break;                        
            }           
        }            
    }      `
pzfprimi

pzfprimi2#

我有一部分工作要做,我可以将值放入dganalysis,过滤它们,并使用作为第二个查询的参数。在运行第二个查询之后,第一个datagridview将包含3列而不是一列。第二个datagrid视图将包含3行而不是2行,并且查询的解析没有填充第二个查询。
第一次跑步:第一次跑步
第二轮:第二轮
以下是更新的代码:

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

namespace Stored_Query_3
{
public partial class frmMain : Form
{
    private string sql;
    private static string namedgAnalysis;
    private string namedgComponents;
    //private string name;
    //private string name;
    public AdvancedDataGridView DVG = new AdvancedDataGridView();
    private string DVGcomponent;
    BindingSource bs;
    DataTable dt = new DataTable();       
    private static string connString = ";Password=;" +
           "Data Source=:1521/;Pooling=false;";

    public frmMain()
    {
        InitializeComponent();
    }      
    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 cmdtb1pg1_Click(object sender, EventArgs e)
    {
        bs.Clear();
        // Get Analysis
        sql = "SELECT DISTINCT NAME FROM LWPROD.ANALYSIS ORDER BY 1";
        bs.DataSource = typeof(AdvancedDataGridView);
        GetData(sql, "namedgAnalysis", dgAnalysis);
        dgAnalysis.ClearSelection();            
    }    
    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
        {               
        }      
    }
    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";

        bs.DataSource = typeof(AdvancedDataGridView);
        GetData(sql, "namedgComponents", dgComponents);
        dgComponents.ScrollBars = ScrollBars.Both;
        dgComponents.ClearSelection();
    }        
    private void GetData(string sql, string name, AdvancedDataGridView Name)
    {
        //clear up string name
        name = name.Remove(0, 4);
        name = name.Trim();

        try
        {
            //oracle connection object
            using (OracleConnection conn = new OracleConnection(connString))
            {
                //retrieve the SQL Server instance version
                sql = sql;  // "SELECT DISTINCT NAME FROM LWPROD.ANALYSIS ORDER BY 1";

                OracleCommand cmd = new OracleCommand(sql, conn);

                //Set the SqlDataAdapter object
                OracleDataAdapter dAdapter = new OracleDataAdapter(cmd);

                //fill dataset with query results
                dAdapter.Fill(dt);
                bs.DataSource = dt;

                //close connection
                conn.Close();
            }
        }
        catch (Exception ex)
        {
            //display error message
            MessageBox.Show("Exception: " + ex.Message);
        }
        finally
        {
            switch (name)
            {
                case "dgAnalysis":
                    //set DataGridView control to read-only
                    dgAnalysis.ReadOnly = true;
                    //set the DataGridView control's data source/data table
                    dgAnalysis.DataSource = bs;
                    break;
                case "dgComponents":
                    //set DataGridView control to read-only
                    dgComponents.ReadOnly = true;
                    //set the DataGridView control's data source/data table
                    dgComponents.DataSource = bs;
                    break;                 
                default:
                    MessageBox.Show("Something Went Wrong");
                    break;
            }               
        }
    }
  }
}

我不知道如何清除数据源和数据网格?我有tried:clearing the bindingsource并重置datagridview的数据源。

相关问题