Adding data to datagridview without losing existing rows










0















hello every body i am new to c# and now im trying to add rows to datagridview using query and invoke but the problem is whenever i try to insert data to table in rewrites all rows so just one row that has the last verified value shown in the rows. i have tried so many thing but i dont know where the problem is. this is my code:



 XDocument xdoc = XDocument.Load("demo.xml");
var query = from key in xdoc.Descendants("user")
where key != null && (key.Element("clientno").Value == recieveddata)
select new

//listBox3.Items.Add(key.Element("name")+""+key.Element("lastname"));
نام = key.Element("name").Value +" "+ key.Element("lastname").Value,
// خانوادگی = key.Element("lastname").Value,
ورزش = key.Element("noeozviat").Value,
تاریخ = key.Element("date").Value,
عضویت = key.Element("duration").Value,
جلسات = key.Element("jalasat").Value
;dataGridView1.Invoke(new Action(() => dataGridView1.DataSource = query.ToList()));


please help me on this.










share|improve this question






















  • I would recommend to never user something like "تاریخ " in code! By using standard characters everyone can read your code.

    – Felix Quehl
    Nov 13 '18 at 13:00











  • thank you for advice but it is not the problem the problem is that every time the query get just one data but i want to add this data to another row not the row that existed before.

    – Hamed Pourgholy
    Nov 13 '18 at 13:02















0















hello every body i am new to c# and now im trying to add rows to datagridview using query and invoke but the problem is whenever i try to insert data to table in rewrites all rows so just one row that has the last verified value shown in the rows. i have tried so many thing but i dont know where the problem is. this is my code:



 XDocument xdoc = XDocument.Load("demo.xml");
var query = from key in xdoc.Descendants("user")
where key != null && (key.Element("clientno").Value == recieveddata)
select new

//listBox3.Items.Add(key.Element("name")+""+key.Element("lastname"));
نام = key.Element("name").Value +" "+ key.Element("lastname").Value,
// خانوادگی = key.Element("lastname").Value,
ورزش = key.Element("noeozviat").Value,
تاریخ = key.Element("date").Value,
عضویت = key.Element("duration").Value,
جلسات = key.Element("jalasat").Value
;dataGridView1.Invoke(new Action(() => dataGridView1.DataSource = query.ToList()));


please help me on this.










share|improve this question






















  • I would recommend to never user something like "تاریخ " in code! By using standard characters everyone can read your code.

    – Felix Quehl
    Nov 13 '18 at 13:00











  • thank you for advice but it is not the problem the problem is that every time the query get just one data but i want to add this data to another row not the row that existed before.

    – Hamed Pourgholy
    Nov 13 '18 at 13:02













0












0








0








hello every body i am new to c# and now im trying to add rows to datagridview using query and invoke but the problem is whenever i try to insert data to table in rewrites all rows so just one row that has the last verified value shown in the rows. i have tried so many thing but i dont know where the problem is. this is my code:



 XDocument xdoc = XDocument.Load("demo.xml");
var query = from key in xdoc.Descendants("user")
where key != null && (key.Element("clientno").Value == recieveddata)
select new

//listBox3.Items.Add(key.Element("name")+""+key.Element("lastname"));
نام = key.Element("name").Value +" "+ key.Element("lastname").Value,
// خانوادگی = key.Element("lastname").Value,
ورزش = key.Element("noeozviat").Value,
تاریخ = key.Element("date").Value,
عضویت = key.Element("duration").Value,
جلسات = key.Element("jalasat").Value
;dataGridView1.Invoke(new Action(() => dataGridView1.DataSource = query.ToList()));


please help me on this.










share|improve this question














hello every body i am new to c# and now im trying to add rows to datagridview using query and invoke but the problem is whenever i try to insert data to table in rewrites all rows so just one row that has the last verified value shown in the rows. i have tried so many thing but i dont know where the problem is. this is my code:



 XDocument xdoc = XDocument.Load("demo.xml");
var query = from key in xdoc.Descendants("user")
where key != null && (key.Element("clientno").Value == recieveddata)
select new

//listBox3.Items.Add(key.Element("name")+""+key.Element("lastname"));
نام = key.Element("name").Value +" "+ key.Element("lastname").Value,
// خانوادگی = key.Element("lastname").Value,
ورزش = key.Element("noeozviat").Value,
تاریخ = key.Element("date").Value,
عضویت = key.Element("duration").Value,
جلسات = key.Element("jalasat").Value
;dataGridView1.Invoke(new Action(() => dataGridView1.DataSource = query.ToList()));


please help me on this.







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 12:57









Hamed PourgholyHamed Pourgholy

156




156












  • I would recommend to never user something like "تاریخ " in code! By using standard characters everyone can read your code.

    – Felix Quehl
    Nov 13 '18 at 13:00











  • thank you for advice but it is not the problem the problem is that every time the query get just one data but i want to add this data to another row not the row that existed before.

    – Hamed Pourgholy
    Nov 13 '18 at 13:02

















  • I would recommend to never user something like "تاریخ " in code! By using standard characters everyone can read your code.

    – Felix Quehl
    Nov 13 '18 at 13:00











  • thank you for advice but it is not the problem the problem is that every time the query get just one data but i want to add this data to another row not the row that existed before.

    – Hamed Pourgholy
    Nov 13 '18 at 13:02
















I would recommend to never user something like "تاریخ " in code! By using standard characters everyone can read your code.

– Felix Quehl
Nov 13 '18 at 13:00





I would recommend to never user something like "تاریخ " in code! By using standard characters everyone can read your code.

– Felix Quehl
Nov 13 '18 at 13:00













thank you for advice but it is not the problem the problem is that every time the query get just one data but i want to add this data to another row not the row that existed before.

– Hamed Pourgholy
Nov 13 '18 at 13:02





thank you for advice but it is not the problem the problem is that every time the query get just one data but i want to add this data to another row not the row that existed before.

– Hamed Pourgholy
Nov 13 '18 at 13:02












1 Answer
1






active

oldest

votes


















0














You are creating a new list and setting it as the data source every time. What you should be doing is creating a class for each element (at the moment you are using an anonymous type), then ADD new elements when you run the query and set this list as the new data source.



Something like this:



class Thing

public string نام get;set;
// Etc


List<Thing> items = new List<Thing>(); // <- should be a class member variable, not a local variable

XDocument xdoc = XDocument.Load("demo.xml");
var newItems = from key in xdoc.Descendants("user")
where key != null && (key.Element("clientno").Value == recieveddata)
select new Thing

نام = key.Element("name").Value +" "+ key.Element("lastname").Value,
ورزش = key.Element("noeozviat").Value,
تاریخ = key.Element("date").Value,
عضویت = key.Element("duration").Value,
جلسات = key.Element("jalasat").Value
;
items.AddRange(newItems.ToList());
dataGridView1.Invoke(new Action(() => dataGridView1.DataSource = items));





share|improve this answer























  • thanks can you explain more i did exactly like you said but nothing is shown in datagridview

    – Hamed Pourgholy
    Nov 13 '18 at 13:08











  • How many items are in items when you step through the code? Also, you will have to change your datagrid to extract the correct properties of the items.

    – Neil
    Nov 13 '18 at 13:17











  • getting error while using items.AddRange(newItems.ToList()); this is error i get Error 26 The best overloaded method match for 'System.Collections.Generic.List<GymAttendaceSystem.Thing>.AddRange(System.Collections.Generic.IEnumerable<GymAttendaceSystem.Thing>)' has some invalid arguments c:usershameddocumentsvisual studio 2013projectsgymattendacesystemgymattendacesystemform1.cs 176 14 GymAttendaceSystem

    – Hamed Pourgholy
    Nov 13 '18 at 13:20











  • What type is newItems?

    – Neil
    Nov 13 '18 at 13:23











  • i made the error fixed but the problem is the same as it was whenever i want to add new data to datagridview it removes the data that were before in the rows and adds new row with one single row that is returned from query

    – Hamed Pourgholy
    Nov 13 '18 at 13:31










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281533%2fadding-data-to-datagridview-without-losing-existing-rows%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You are creating a new list and setting it as the data source every time. What you should be doing is creating a class for each element (at the moment you are using an anonymous type), then ADD new elements when you run the query and set this list as the new data source.



Something like this:



class Thing

public string نام get;set;
// Etc


List<Thing> items = new List<Thing>(); // <- should be a class member variable, not a local variable

XDocument xdoc = XDocument.Load("demo.xml");
var newItems = from key in xdoc.Descendants("user")
where key != null && (key.Element("clientno").Value == recieveddata)
select new Thing

نام = key.Element("name").Value +" "+ key.Element("lastname").Value,
ورزش = key.Element("noeozviat").Value,
تاریخ = key.Element("date").Value,
عضویت = key.Element("duration").Value,
جلسات = key.Element("jalasat").Value
;
items.AddRange(newItems.ToList());
dataGridView1.Invoke(new Action(() => dataGridView1.DataSource = items));





share|improve this answer























  • thanks can you explain more i did exactly like you said but nothing is shown in datagridview

    – Hamed Pourgholy
    Nov 13 '18 at 13:08











  • How many items are in items when you step through the code? Also, you will have to change your datagrid to extract the correct properties of the items.

    – Neil
    Nov 13 '18 at 13:17











  • getting error while using items.AddRange(newItems.ToList()); this is error i get Error 26 The best overloaded method match for 'System.Collections.Generic.List<GymAttendaceSystem.Thing>.AddRange(System.Collections.Generic.IEnumerable<GymAttendaceSystem.Thing>)' has some invalid arguments c:usershameddocumentsvisual studio 2013projectsgymattendacesystemgymattendacesystemform1.cs 176 14 GymAttendaceSystem

    – Hamed Pourgholy
    Nov 13 '18 at 13:20











  • What type is newItems?

    – Neil
    Nov 13 '18 at 13:23











  • i made the error fixed but the problem is the same as it was whenever i want to add new data to datagridview it removes the data that were before in the rows and adds new row with one single row that is returned from query

    – Hamed Pourgholy
    Nov 13 '18 at 13:31















0














You are creating a new list and setting it as the data source every time. What you should be doing is creating a class for each element (at the moment you are using an anonymous type), then ADD new elements when you run the query and set this list as the new data source.



Something like this:



class Thing

public string نام get;set;
// Etc


List<Thing> items = new List<Thing>(); // <- should be a class member variable, not a local variable

XDocument xdoc = XDocument.Load("demo.xml");
var newItems = from key in xdoc.Descendants("user")
where key != null && (key.Element("clientno").Value == recieveddata)
select new Thing

نام = key.Element("name").Value +" "+ key.Element("lastname").Value,
ورزش = key.Element("noeozviat").Value,
تاریخ = key.Element("date").Value,
عضویت = key.Element("duration").Value,
جلسات = key.Element("jalasat").Value
;
items.AddRange(newItems.ToList());
dataGridView1.Invoke(new Action(() => dataGridView1.DataSource = items));





share|improve this answer























  • thanks can you explain more i did exactly like you said but nothing is shown in datagridview

    – Hamed Pourgholy
    Nov 13 '18 at 13:08











  • How many items are in items when you step through the code? Also, you will have to change your datagrid to extract the correct properties of the items.

    – Neil
    Nov 13 '18 at 13:17











  • getting error while using items.AddRange(newItems.ToList()); this is error i get Error 26 The best overloaded method match for 'System.Collections.Generic.List<GymAttendaceSystem.Thing>.AddRange(System.Collections.Generic.IEnumerable<GymAttendaceSystem.Thing>)' has some invalid arguments c:usershameddocumentsvisual studio 2013projectsgymattendacesystemgymattendacesystemform1.cs 176 14 GymAttendaceSystem

    – Hamed Pourgholy
    Nov 13 '18 at 13:20











  • What type is newItems?

    – Neil
    Nov 13 '18 at 13:23











  • i made the error fixed but the problem is the same as it was whenever i want to add new data to datagridview it removes the data that were before in the rows and adds new row with one single row that is returned from query

    – Hamed Pourgholy
    Nov 13 '18 at 13:31













0












0








0







You are creating a new list and setting it as the data source every time. What you should be doing is creating a class for each element (at the moment you are using an anonymous type), then ADD new elements when you run the query and set this list as the new data source.



Something like this:



class Thing

public string نام get;set;
// Etc


List<Thing> items = new List<Thing>(); // <- should be a class member variable, not a local variable

XDocument xdoc = XDocument.Load("demo.xml");
var newItems = from key in xdoc.Descendants("user")
where key != null && (key.Element("clientno").Value == recieveddata)
select new Thing

نام = key.Element("name").Value +" "+ key.Element("lastname").Value,
ورزش = key.Element("noeozviat").Value,
تاریخ = key.Element("date").Value,
عضویت = key.Element("duration").Value,
جلسات = key.Element("jalasat").Value
;
items.AddRange(newItems.ToList());
dataGridView1.Invoke(new Action(() => dataGridView1.DataSource = items));





share|improve this answer













You are creating a new list and setting it as the data source every time. What you should be doing is creating a class for each element (at the moment you are using an anonymous type), then ADD new elements when you run the query and set this list as the new data source.



Something like this:



class Thing

public string نام get;set;
// Etc


List<Thing> items = new List<Thing>(); // <- should be a class member variable, not a local variable

XDocument xdoc = XDocument.Load("demo.xml");
var newItems = from key in xdoc.Descendants("user")
where key != null && (key.Element("clientno").Value == recieveddata)
select new Thing

نام = key.Element("name").Value +" "+ key.Element("lastname").Value,
ورزش = key.Element("noeozviat").Value,
تاریخ = key.Element("date").Value,
عضویت = key.Element("duration").Value,
جلسات = key.Element("jalasat").Value
;
items.AddRange(newItems.ToList());
dataGridView1.Invoke(new Action(() => dataGridView1.DataSource = items));






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 13:03









NeilNeil

4,74411537




4,74411537












  • thanks can you explain more i did exactly like you said but nothing is shown in datagridview

    – Hamed Pourgholy
    Nov 13 '18 at 13:08











  • How many items are in items when you step through the code? Also, you will have to change your datagrid to extract the correct properties of the items.

    – Neil
    Nov 13 '18 at 13:17











  • getting error while using items.AddRange(newItems.ToList()); this is error i get Error 26 The best overloaded method match for 'System.Collections.Generic.List<GymAttendaceSystem.Thing>.AddRange(System.Collections.Generic.IEnumerable<GymAttendaceSystem.Thing>)' has some invalid arguments c:usershameddocumentsvisual studio 2013projectsgymattendacesystemgymattendacesystemform1.cs 176 14 GymAttendaceSystem

    – Hamed Pourgholy
    Nov 13 '18 at 13:20











  • What type is newItems?

    – Neil
    Nov 13 '18 at 13:23











  • i made the error fixed but the problem is the same as it was whenever i want to add new data to datagridview it removes the data that were before in the rows and adds new row with one single row that is returned from query

    – Hamed Pourgholy
    Nov 13 '18 at 13:31

















  • thanks can you explain more i did exactly like you said but nothing is shown in datagridview

    – Hamed Pourgholy
    Nov 13 '18 at 13:08











  • How many items are in items when you step through the code? Also, you will have to change your datagrid to extract the correct properties of the items.

    – Neil
    Nov 13 '18 at 13:17











  • getting error while using items.AddRange(newItems.ToList()); this is error i get Error 26 The best overloaded method match for 'System.Collections.Generic.List<GymAttendaceSystem.Thing>.AddRange(System.Collections.Generic.IEnumerable<GymAttendaceSystem.Thing>)' has some invalid arguments c:usershameddocumentsvisual studio 2013projectsgymattendacesystemgymattendacesystemform1.cs 176 14 GymAttendaceSystem

    – Hamed Pourgholy
    Nov 13 '18 at 13:20











  • What type is newItems?

    – Neil
    Nov 13 '18 at 13:23











  • i made the error fixed but the problem is the same as it was whenever i want to add new data to datagridview it removes the data that were before in the rows and adds new row with one single row that is returned from query

    – Hamed Pourgholy
    Nov 13 '18 at 13:31
















thanks can you explain more i did exactly like you said but nothing is shown in datagridview

– Hamed Pourgholy
Nov 13 '18 at 13:08





thanks can you explain more i did exactly like you said but nothing is shown in datagridview

– Hamed Pourgholy
Nov 13 '18 at 13:08













How many items are in items when you step through the code? Also, you will have to change your datagrid to extract the correct properties of the items.

– Neil
Nov 13 '18 at 13:17





How many items are in items when you step through the code? Also, you will have to change your datagrid to extract the correct properties of the items.

– Neil
Nov 13 '18 at 13:17













getting error while using items.AddRange(newItems.ToList()); this is error i get Error 26 The best overloaded method match for 'System.Collections.Generic.List<GymAttendaceSystem.Thing>.AddRange(System.Collections.Generic.IEnumerable<GymAttendaceSystem.Thing>)' has some invalid arguments c:usershameddocumentsvisual studio 2013projectsgymattendacesystemgymattendacesystemform1.cs 176 14 GymAttendaceSystem

– Hamed Pourgholy
Nov 13 '18 at 13:20





getting error while using items.AddRange(newItems.ToList()); this is error i get Error 26 The best overloaded method match for 'System.Collections.Generic.List<GymAttendaceSystem.Thing>.AddRange(System.Collections.Generic.IEnumerable<GymAttendaceSystem.Thing>)' has some invalid arguments c:usershameddocumentsvisual studio 2013projectsgymattendacesystemgymattendacesystemform1.cs 176 14 GymAttendaceSystem

– Hamed Pourgholy
Nov 13 '18 at 13:20













What type is newItems?

– Neil
Nov 13 '18 at 13:23





What type is newItems?

– Neil
Nov 13 '18 at 13:23













i made the error fixed but the problem is the same as it was whenever i want to add new data to datagridview it removes the data that were before in the rows and adds new row with one single row that is returned from query

– Hamed Pourgholy
Nov 13 '18 at 13:31





i made the error fixed but the problem is the same as it was whenever i want to add new data to datagridview it removes the data that were before in the rows and adds new row with one single row that is returned from query

– Hamed Pourgholy
Nov 13 '18 at 13:31



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281533%2fadding-data-to-datagridview-without-losing-existing-rows%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo