0

我正在使用 windows 窗体,我想创建一个方法,该方法将根据datagridview中 ComboBox 内的项目简单地查看所有数据。

private void InsertReceipt()
{
        decimal Stub;

        Stub = decimal.Parse(txtAmount.Text) / 2000;

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "INSERT INTO Ticket(CustomerID, Date, Store, Amount, NoStub)" +
                   "VALUES (@CustomerID, @Date, @Store, @Amount, @NoStub) ";
        cmd.Parameters.AddWithValue("@CustomerID", cboName.SelectedValue);
        cmd.Parameters.AddWithValue("@Date", dtpDate.Value.Date.ToString());
        cmd.Parameters.AddWithValue("@Store", txtStore.Text);
        decimal amount = decimal.Parse(txtAmount.Text);
        cmd.Parameters.AddWithValue("@Amount", amount);
        cmd.Parameters.Add("@NoStub", SqlDbType.Decimal).Value = Stub;

        cmd.ExecuteNonQuery();
}

这是字段,我需要根据 ComboBox 中的项目查看所有数据。

4

2 回答 2

0

使用此方法将 Gridview 与数据库中的数据绑定

protected void BindGridview()
{
using (SqlConnection con = new SqlConnection("Data Source=DatabaseName;Integrated Security=true;Initial Catalog=***"))//Connection string
{
con.Open();
SqlCommand cmd = new SqlCommand("Select CustomerID,Date,Store,Amount,NoStub  FROM Ticket where ColumnName='"+ YourDrodownId.SlectedValue +"'", con);
SqlDataReader dr = cmd.ExecuteReader();
YourGridview.DataSource = dr;
YourGridview.DataBind();
con.Close();
}
}

然后在我们的 Gridview 控件中设置为autogeneratecolumns propertyfalse并在页面加载或您想要的内部调用此方法!

这是带有代码的完整示例!

http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=2

http://forums.asp.net/t/1904884.aspx/1

更新:

在桌面应用程序中:

http://www.freedotnetapps.com/sql/database-operations-and-datagridview-bind-in-net-desktop-application/

于 2013-07-09T08:13:13.300 回答
0

您的问题非常笼统且含糊不清,因此很难准确回答。如果你只是想学习如何在windows 窗体中使用datagridview ,你可以在网上找到很多关于它的信息。

我发现dotnetpearls是一个很好的起点

void FillData()
{
    // 1
    // Open connection
    using (SqlCeConnection c = new SqlCeConnection(
    Properties.Settings.Default.DataConnectionString))
    {
    c.Open();
    // 2
    // Create new DataAdapter
    using (SqlCeDataAdapter a = new SqlCeDataAdapter(
        "SELECT * FROM Animals", c))
    {
        // 3
        // Use DataAdapter to fill DataTable
        DataTable t = new DataTable();
        a.Fill(t);
        // 4
        // Render data onto the screen
        dataGridView1.DataSource = t;
    }
    }
}

如果我可以建议您通过一些教程工作并询问您遇到的具体问题(带有错误消息等)?

于 2013-07-09T08:34:06.393 回答