C#Datagrid搜索问题 [英] C# Datagrid search problem

查看:49
本文介绍了C#Datagrid搜索问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MSSQL连接到数据库后,我的代码中存在关于搜索的问题.这个想法是编辑数据库(添加,更新,删除)并搜索数据库.通过编辑我可以,但是通过搜索我什么也不能做.我什至不知道如何开始.该数据库有两列,一列是数字,另一列是文本.

对不起,我的英文,非常感谢.

I have a problem in my code about search after connecting to a database with MSSQL. The idea was to edit the database (add,update,delete) and also to search the database. With the edit i''m ok but with the search i can''t do anything. I don''t know even how to start. The database has two columns, one with numbers and the other is text.

Sorry for my english and thank you very much.

推荐答案

首先,这部分代码似乎是不必要的:
Firstly, this part of your code seems to be unnecessary:
foreach (DataColumn dc in userTable.Columns)
{
      DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
      column.DataPropertyName = dc.ColumnName;
      column.HeaderText = dc.ColumnName;
      column.Name = dc.ColumnName;
      column.SortMode = DataGridViewColumnSortMode.Automatic;
      column.ValueType = dc.DataType;
      userDataGridView.Columns.Add(column);
}



因为您正在使用userTable中的所有列.如果您只想要其中的一些,那将是一个合理的方法.除非您将其<code> AutoGenerateColumns属性设置为false,否则DataGridView将自动为您添加列.

其次,您的代码不会显示DataGridview设置了DataSource属性的位置(顶部附近除外,该位置设置为null).

不过,无论在哪里完成,都请尝试此操作.
在表单中添加一个BindingSource组件,(出于解释的目的,我将其称为userBindingSource).在设计器中,将DataGridView的DataSource设置为此新的userBindingSource.从此以后,您将永远不会更改此设置,以后所有更改都将对userBindingSource进行.

然后在代码中的任意位置设置userDataGridView的DataSource属性,例如



because you are using all of the columns from userTable. If you only wanted a few of them, then that would be a reasonable way to do it. The DataGridView will automatically add the columns for you, unless you have set its <code>AutoGenerateColumns property to false.

Secondly, your code does not show where the DataGridview has its DataSource property set (except near the top, where it is set to null).

Still, wherever that is done try this.
Add a BindingSource component to your form, (for the purpose of this explanation I''ll call it userBindingSource). In the designer, set the DataSource for your DataGridView to this new userBindingSource. From here on you will never change this, all future changes will be done to userBindingSource.

Then wherever in your code you set the DataSource property of userDataGridView, e.g.

userDataGridView.DataSource = null;

// OR
userDataGridView.DataSource = userTable;



将其更改为:



change those to:

userBindingSource.DataSource = null;

// AND
userBindingSource.DataSource = userTable;



确保更改所有这些内容,否则将无法正常工作.

然后,假设您的应用程序仍然可以像现在那样运行,则可以使用 BindingSource.Find [ ^ ]方法来查找行的索引,并将DataGridView的CurrentRow设置为该索引.

BindingSource还具有MoveFirstMoveLastMoveNextMovePrevious方法,这些方法将适合您应用程序中的按钮.



Make sure that you change all of them, otherwise it won''t work.

Then, assuming that your application still works as it does now, you will be able to use the BindingSource.Find[^] method to locate the index of the row, and set CurrentRow for the DataGridView to that index.

BindingSource also has MoveFirst, MoveLast, MoveNext and MovePrevious methods which will fit the buttons that you have in your app.


这里有几个非常好的链接对于初学者:


1. [ 2. [
Here are a couple of really good links for starters:


1.[^]

2.[^]


非常感谢您的回答,但是这些文章对我没有帮助.他们指的是如何连接,编辑,删除,更新",而我已经做到了.我在通过数据库进行搜索时遇到问题,找不到任何说明其操作方法的文章.
Thank you very much for your answer, but these articles can not help me. They refer at "how to connect,edit,delete,update" and i already have done these. I have a problem with the search through the database and i can not find anywhere an article that explains how to do it.


这篇关于C#Datagrid搜索问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆