数据网格视图光标

发布于 2024-12-10 17:50:07 字数 1292 浏览 3 评论 0原文

我的数据网格显示来自我的 sqlBd 的表。我为每列添加一个文本框来显示每行。

这是我的代码:

 private void CustomerViewToolStripMenuItem_Click(object sender, EventArgs e)
    {
        BindingSource Clients_bs = new BindingSource();
        SqlConnection con = new SqlConnection(dc.Con);
        con.Open();
        da.SelectCommand = new SqlCommand("SELECT * FROM Clients", con);

        da.Fill(dt);
        con.Close();

        dgCustomers.DataSource = dt;
        Clients_bs.DataSource = dt;

       txtBoxIdCustomers.DataBindings.Add(new Binding("Text", Clients_bs,"id_Client"));                

        txtBoxFullName.DataBindings.Add(new Binding("Text", Clients_bs, "prénom_Nom"));
        txtBoxAddress.DataBindings.Add(new Binding("Text", Clients_bs, "adresse"));
        txtBoxCity.DataBindings.Add(new Binding("Text", Clients_bs, "ville"));
        txtBoxProvince.DataBindings.Add(new Binding("Text", Clients_bs, "province"));
        txtBoxPostal.DataBindings.Add(new Binding("Text", Clients_bs, "code_Postal"));
        txtBoxPhone.DataBindings.Add(new Binding("Text", Clients_bs, "numéro_Teléphone"));
    }   

它像 SQL 中的表一样填充我的 dataGrid,并且我的 txtbox 仅显示光标指向 dataGrid 中第一行的第一行。

我想移动第二行数据网格的光标,并在第二行自动显示txtbox的绑定....

我可以让一个按钮上一个和下一个,但不能让光标...

谢谢你帮助我!

My datagrid show a table from my sqlBd. I add a textBox for each column that for show each rows.

This is my code:

 private void CustomerViewToolStripMenuItem_Click(object sender, EventArgs e)
    {
        BindingSource Clients_bs = new BindingSource();
        SqlConnection con = new SqlConnection(dc.Con);
        con.Open();
        da.SelectCommand = new SqlCommand("SELECT * FROM Clients", con);

        da.Fill(dt);
        con.Close();

        dgCustomers.DataSource = dt;
        Clients_bs.DataSource = dt;

       txtBoxIdCustomers.DataBindings.Add(new Binding("Text", Clients_bs,"id_Client"));                

        txtBoxFullName.DataBindings.Add(new Binding("Text", Clients_bs, "prénom_Nom"));
        txtBoxAddress.DataBindings.Add(new Binding("Text", Clients_bs, "adresse"));
        txtBoxCity.DataBindings.Add(new Binding("Text", Clients_bs, "ville"));
        txtBoxProvince.DataBindings.Add(new Binding("Text", Clients_bs, "province"));
        txtBoxPostal.DataBindings.Add(new Binding("Text", Clients_bs, "code_Postal"));
        txtBoxPhone.DataBindings.Add(new Binding("Text", Clients_bs, "numéro_Teléphone"));
    }   

That fill my dataGrid like my table in sql, and my txtbox's shows the first row only whit the cursor pointing on the first row in my dataGrid.

I want to move the cursor of the data grid on the second row and show txtbox's bind automatically on the second row ....

I can make whit a button previous and next, but not whit the cursor...

Thank's for helping me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Hello爱情风 2024-12-17 17:50:07

而不是这样:这样

dgCustomers.DataSource = dt;
Clients_bs.DataSource = dt;

做:

Clients_bs.DataSource = dt;
dgCustomers.DataSource = Clients_bs;

那么它应该可以工作,因为文本框和网格具有相同的 BindingSource ;-)

编辑:

另外,请不要这样做:

SqlConnection con = new SqlConnection(dc.Con);
con.Open();
da.SelectCommand = new SqlCommand("SELECT * FROM Clients", con);

da.Fill(dt);
con.Close();

这样做:

using(var con = new SqlConnection(dc.Con))
{
    con.Open();
    da.SelectCommand = new SqlCommand("SELECT * FROM Clients", con);

    da.Fill(dt);
}

instead of this:

dgCustomers.DataSource = dt;
Clients_bs.DataSource = dt;

do this:

Clients_bs.DataSource = dt;
dgCustomers.DataSource = Clients_bs;

then it should work as the TextBoxes and the Grid have the same BindingSource ;-)

Edit:

and also, please, instead of this:

SqlConnection con = new SqlConnection(dc.Con);
con.Open();
da.SelectCommand = new SqlCommand("SELECT * FROM Clients", con);

da.Fill(dt);
con.Close();

do this:

using(var con = new SqlConnection(dc.Con))
{
    con.Open();
    da.SelectCommand = new SqlCommand("SELECT * FROM Clients", con);

    da.Fill(dt);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文