Need to set datagridview datasource with a binding source

筅森魡賤 提交于 2021-01-29 20:14:18

问题


I'm using an advanced datagridview and to use the filters that come with it you need to use a binding source. I'm writing an Oracle query (actually several of them) and using the results as a datasource. I can't seem to get it to work correctly. I have googled all the solutions and have tried them all with no success.

My code:

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

回答1:


Here is some updated code. This is still not working. In this attempt.

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;                        
            }           
        }            
    }      `



回答2:


I got part of this to work, I can get values into the dgAnalysis, filter them and use the as parameters for the second query. After running the second query the first data gridview will contain 3 columns instead of one. The second datagrid view will contain 3 rows instead of 2 and parse of the query didn't populate the second query.

Fist run: First run

Second run: Second run

Here is the updated code:

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

I'm not sure how to clear the binsindsources and datagrids? I have tried:Clearing the bindingsource and resetting the datasource of the datagridview.



来源:https://stackoverflow.com/questions/62027985/need-to-set-datagridview-datasource-with-a-binding-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!