【问题标题】:DataGridView playing stubborn. It just wouldn't bindDataGridView 打顽固。它只是不会绑定
【发布时间】:2011-06-07 15:51:56
【问题描述】:

我有一个顽固的数据网格视图拒绝显示绑定数据。 我放置了一个名为展览网格视图的网格视图并将其数据源设置为无。然后我添加了一个独立的数据源,它可以将列返回到网格中,但首先网格中显示的数据将基于从下拉列表中选择的内容。从下面的图片中查看。

所以基本上从caseid标签旁边的下拉列表中选择了一些项目,并且网格相应地显示值......因为我需要一个selectedIndexchanged方法所以我在我的page.cs中有这个

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateDataSet();

            caseID = DropDownList1.SelectedItem.Value.Trim();

        DataView exhibitDataView = new DataView(exhibitDataSet.Tables[0]);
        exhibitDataView.RowFilter = "FilingID = '" + caseID + "' ";
        ExhibitGridView.DataSource = exhibitDataView;
        ExhibitGridView.DataBind();
    }
    private void CreateDataSet()
    {
        exhibitConnection.ConnectionString =
        ExhibitListSqlDataSource.ConnectionString;
        exhibitSqlDataAdapter.SelectCommand = new
        SqlCommand(ExhibitListSqlDataSource.SelectCommand, exhibitConnection);
        exhibitSqlDataAdapter.Fill(exhibitDataSet);
    }

代码运行良好...我插入了一个断点以确保实际返回一些数据以进行绑定并且存在...您可以从下面的屏幕截图中看到:

直到 (ExhibitGridView.DataBind())。因此,当我运行下一个块时,我希望数据能够绑定并显示在浏览器中,但由于某些未知原因,gridview 表现得很顽固。我尝试直接指定数据源,它在页面加载时显示成功,否则它不会响应。

可能是什么原因?

【问题讨论】:

    标签: c# asp.net sql-server data-binding datagridview


    【解决方案1】:

    我确实认为您需要为 DataAdapter 提供您为 select 语句提供的参数。看看吧。

    我已经从我的代码中为您提供了一个使用 OleDB 的示例(为了便于阅读,我删除了所有打开/关闭连接)。它们非常相似。

    SqlCmd = "select * from App_Details WHERE App_Name LIKE @Var";
    
    aCommand = new OleDbCommand(SqlCmd, aConnection);
    aCommand.Parameters.AddWithValue("@Var", value);
    
    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(SqlCmd, aConnection);
    OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(dataAdapter);
    
    // Now I do not see this part in your code right before you bind your data
    dataAdapter.SelectCommand.Parameters.AddWithValue("@Var", value);
    DataTable table = new DataTable();
    dataAdapter.Fill(table);
    dgvSearchApp.DataSource = table;
    

    【讨论】:

      【解决方案2】:

      确保回发事件。也许该页面正在进行两次回发。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-13
        • 2013-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多