【发布时间】:2020-09-13 14:25:31
【问题描述】:
我正在使用高级数据网格视图,要使用它附带的过滤器,您需要使用绑定源。我正在编写一个 Oracle 查询(实际上是其中的几个)并将结果用作数据源。我似乎无法让它正常工作。我用谷歌搜索了所有解决方案,并尝试了所有方法,但均未成功。
我的代码:
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;
}
}
}
【问题讨论】:
-
您是否尝试调试 finally 块中发生的事情?你确定这些名字吗?
-
GetData 还返回一个 BindingSource(在任何地方都使用相同的全局变量),因此您不必将 BindingSource 分配给同一 BindingSource 的 DataSource。无需从 GetData 分配返回
-
Steve 此代码 private BindingSource GetData(string sql, DataGridView DGV) 需要返回。我将如何改变它?
-
史蒂夫 - 是的,我很确定名字。
-
bs = GetData(sql, dgAnalysis); 但这是不必要的,因为 bs 已在 GetData 中设置。当然在修复@JQSOFT 指出的错误之后
标签: c# sql oracle datagridview datasource