Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

c# - Adding rows programatically to a DataGridView that is data bound?

I have a problem and I can't seem to be able to solve it. I thought someone here will be able to help out.

I have a form that has a DataGridView of customers. Now, I want to add several customers to this DataGridView without actually adding them to the database. This is because the client must be able to create a list of clients and when thats done add them all at once.

I have tried this:

string[] array = {"Microsoft", "Redmond", "Something"}
dataGridView.Rows.Add (array);

Now this can't be done because I get an exception saying something in the lines of you cant add rows programatically to a DataGridView that is data bound.

Now I have also read that this can be solved by using a table adapter to insert the rows instead of directly adding them via. a DGV. But this is also not possible because I use custom headers in the DGV because existing data is fetched via JOIN's so if I add them via a TableAdapter I get an exception that the it doesn't match the databases table schema.

Now I am really lost... Anyone know of a (halfway) elegant solution to this problem?

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to show those rows, just do a UNION (-like) action before the databinding. Ie get your list of records, add the special ones and then databind.

Updating will require some special action(s) too. But with a total lack of details about your datastructures it's impossible to say what.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...