简体   繁体   English

数据网格视图光标

[英]Data Grid View cursor

My datagrid show a table from my sqlBd. 我的datagrid显示了我的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. 像我在sql中的表一样填充了dataGrid,而txtbox则显示第一行,而光标仅指向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 .... 我想在第二行上移动数据网格的光标,并在第二行上自动显示txtbox的绑定...。

I can make whit a button previous and next, but not whit the cursor... 我可以在上一个和下一个按钮上创建一个按钮,但不能在光标上创建一个按钮...

Thank's for helping me! 谢谢您的帮助!

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 ;-) 那么它应该工作,因为TextBoxes和Grid具有相同的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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM