需要使用绑定源设置datagridview数据源 [英] Need to set datagridview datasource with a binding source
问题描述
我使用的是高级datagridview,要使用其随附的过滤器,您需要使用绑定源.我正在编写一个Oracle查询(实际上是其中的几个查询),并将结果用作数据源.我似乎无法使其正常工作.我用Google搜索了所有解决方案,并尝试了所有解决方案,但均未成功.
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.
我的代码:
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;
}
}
}
推荐答案
我可以使用其中的一部分,我可以将值放入dgAnalysis中,对其进行过滤并将其用作第二个查询的参数.运行第二个查询后,第一个数据gridview将包含3列而不是一个列.第二个datagrid视图将包含3行而不是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.
第一拳:首次运行
第二次运行:第二次运行
这是更新的代码:
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;
}
}
}
}
}
我不确定如何清除binsindsources和datagrids?我已经尝试过:清除绑定源并重置datagridview的数据源.
I'm not sure how to clear the binsindsources and datagrids? I have tried:Clearing the bindingsource and resetting the datasource of the datagridview.
这篇关于需要使用绑定源设置datagridview数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!