|
I have added a Text File to my project. Can somebody please tell me what the code is to send, modify and retrieve information - say from text boxes - to that file?
|
|
|
|
|
Have a look at the TextReader , TextWriter and File classes all in the System.IO namespace. They contain everything you need.Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Thanks
|
|
|
|
|
Hi,
I would like to loop through the DataSet at runtime to retrieve selected values and store them in local variables but i am not sure how i could do this using DataSet that is generated using the inbuilt wizard. I am using SQL Server Compact. My database has "*.sdf" extension. I created a strongly typed DataSet using the wizard. So in the solution explorer i can see the DataSet and it has an extension of "*.xsd".
At the moment i am doing this:
foreach(DataGridViewRow dgvrow in CustomerGridView.Rows)
{
String Surname = dgvrow.Cells[2].Value.ToString();
}
Wouldnt it be more efficient if i looped through the DataSet that I have created? If so, how would i go about doing this based on the above code.
If anybody could assist me with this, it would be much appreciated.
Thanks,
|
|
|
|
|
use something like:
ds.TableName[RowIndex].ColumnName;
|
|
|
|
|
Hi,
Thanks but do i not need to modify what is in between the brackets?
foreach(DataGridViewRow dgvrow in CustomerGridView.Rows)
Also how would i access the value from the specified index?
Thanks,
|
|
|
|
|
well offcourse you do.
Either you loop the dvg or the ds.
foreach (DataRow r in ds.Tables["TableName"].Rows)
{
String surname = r.ItemArray[2].ToString();
}
but I'm really not sure that it will be faster.
If you want fast forget about ds and dvg(sure use one for the UI display) and
use a List<MyClass> set the dgv.DataSource = myList;
and then Loop throuh the List or use linq or ....
[Added] the DataRow.ItemArray returns an array of objects => many casts/boxing/unboxim => poor perf
On the other hand if you load the data in a List<myclass> using a datareader => no casting and faster
when loading data, and especially faster when looping/searching/whatever on the List/Datas.[/Added]modified on Wednesday, March 10, 2010 6:40 PM
|
|
|
|
|
Not really. You can loop through properties of your tables since you have typed dataset.
foreach(MyTableRow row in MyTable.Rows)
string surname = row.SurName;
|
|
|
|
|
Hi,
Just a quick question regarding best practice, is it best to loop through the rows like you have stated instead of looping through the datagridview?
Thanks for the input all.
|
|
|
|
|
Basically depends on what you want to achieve.
In case it is looping through all visible rows in datagridview (In case you are using DataView) then DataSet is of no help.
In case it is a basic looping through all the rows, why bother going to datagridview? you need to cast the object back to your datatype. Better would be to loop through TYPED datarow through a TYPED datatable... typed datarow already takes care of the conversion.
|
|
|
|
|
Hi, this is my first posting at this forum and I need some help with my project. I am new to c# and still confused at times ...
My application is as follow:
I have main MDI_ParentForm, from which I open a ChildForm. I use ChildForm as drawing area for my graph.
I am dividing ChildForm up into smaller areas (rectangles). This is done by creating new object:
myDivisionClass myGDI = new myDivisionClass(this) ;
In my class definition for myDivisionClass I create object for each reactangle (for example: first rectangle represents grid area, second title, third labels for traces and so on ...)
myGraphGridClass myGrid = new myGraphGridClass(myRectangle);
myGraphTitle myTitle = new myGraphTitle(myRectangle);
myGraphLabels myTitle = new myGraphLabels(myRectangle);
etc...
Now I want to add a setup form (shown when pressing button on ChildForm), from which I could change properties of my myGrid object from myGraphGridClass .
I would also like to be able to open any number of ChildForms and change properties for each individual ChildForm object (so each could display graph with different settings ) ...
tnx, Pata
|
|
|
|
|
peropata wrote: I would also like to be able to open any number of ChildForms and change properties for each individual ChildForm object (so each could display graph with different settings ) ...
just put the this.Owner == toYourMDIParent for each of the child.
peropata wrote: Now I want to add a setup form (shown when pressing button on ChildForm), from which I could change properties of my myGrid object from myGraphGridClass.
There are many options:
One would be to Create a Custom/User Control that encapsuletes your graph form and in that control put the comboboxes textboxes, etc to alter/modify different properties of the graph
Or if you want a setup/properties button create a new form that deals with the different properties
and in the ctor pass in your graph
eg:
SetupForm sf = new SetupForm(this.myGraph)
or something like this.
|
|
|
|
|
Or if you want a setup/properties button create a new form that deals with the different properties
and in the ctor pass in your graph:
SetupForm sf = new SetupForm(this.myGraph)
This code would work for setting properties defined in myDivisionClass class, and not for properties defined in myGraphGridClass . In this case I would have to transfer values from myDivisionClass object to myGraphGridClass object which is created in myDivisionClass . I want ro know if there is a way to directly change properties defined in myGraphGridClass .
Pata
|
|
|
|
|
I don't know much the inner works of myDivisionClass or myGraphGridClass .
But one way would be to pass the myGraphGridClass too in the ctor. It's an object
so you pass the reference to it. And by modifyng the myGraphGridClass in your SetupForm,
you are actually modifyng the one in your child window(s).
All you need to do is call the Invalidate() or refresh method on the grid or the child form, once you
close the setupForm. Or directly from the setup form. The possibilities are countless and you know
better what you want.
|
|
|
|
|
I found the problem. It was my mistake in creating objects. I was creating object MyGraphGridClass inside one of the method of MyDivisionClass and as such it was destroyed as soon as it left that method and its properties could not be accessed. I rearranged a code and put the declaration (creation) of my object as one of the properties of my MyDivisionClass , so I can access its properties.
class MyGraphGridClass
{
private int myProperty;
public int Property
{
get { return myProperty; }
set { myProperty = value; }
}
.
.
.
}
class MyDivisionClass
{
private MyGraphGridClass objMyGraphGrid = new MyGraphGridClass();
internal MyGraphGridClass MyGraphGrid
{
get { return objMyGraphGrid; }
set { objMyGraphGrid = value; }
}
.
.
.
}
MyDivisionClass Graph_01 = new MyDivisionClass();
MyDivisionClass Graph_02 = new MyDivisionClass();
Graph_01.MyGraphGrid.Property = 5;
Graph_02.MyGraphGrid.Property = 3;
SetupForm sf = new SetupForm(Graph_01.MyGraphGrid);
Pata
|
|
|
|
|
Hello,
Being a newbie, I am still coming to grips with understanding C#. I can use a dataset to create a datagrid etc. and I can programatically connect to a database but what gets me is how would you change the database path and have that reflected in you datasets etc.? Perhaps I am not thinking correctly but what I am looking to achieve is to allow and Admin to change the database path (access backend) should this be necessary and have that propogate so that the dataset and adapter will never err due to an incorrect path.
Thanks for your patience,
Art
|
|
|
|
|
Use like a xml/config file where you store the ConnectionSring.
Without the pass if any.
And create a Utlity method that gets the connection from the config file.
|
|
|
|
|
|
Read some documentation(s)!!!
I would use the XLinq to read data.
OK
public static string GetConnection(){
try{
System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Load("confix.xml");
return doc.Element("Connection").Value;
}
catch{
return "failure";
}
}
<?xml version="1.0" encoding="utf-8" ?>
<Connection>"MyConnectionWithoutPassHere"</Connection>
|
|
|
|
|
Hi,
I have a Windows application, written in c# and in my application, I have a DataGridView that has 2 DataGridViewComboBoxColumns in it, along with a DataGridViewTextBoxColumn. The 2 ComboBoxColumns are each populated by a DataSet. For each row, the user selects an item from each of the ComboBoxColumns and enters a number into the TextBoxColumn. As an example, let's say the Color ComboBox has 3 choices in it: Red, Blue & Green and the Size ComboBox has 3 choices in it: Small, Medium & Large. In our example, for the first row, the user has selected Red from the Color ComboBox, Medium for the Size ComboBox and has entered "5" in the Qty TextBox. For Row 2, the user selected Green in that Color ComboBox, Large in the Size ComboBox and has entered "4" in the Qty TextBox.
Row...........Color(ComboBox).........Size(ComboBox)......Qty(TextBox)
----------------------------------------------------------------------------------
..1............Red...............................Medium......................5
..2............Green...........................Large.........................4
Once the user is done, this information is saved to a table. Let's also assume that this information was for January, 2010.
When the user starts a new month, (February, 2010, for example), their data could be exactly the same as January or it may change a little. If it IS the same or very close to the same, rather than force the user to reenter all of the information as they did in January, I'd like to be able to give them the option to "import" the data from January but still give them all the options that they had in January. What this means is that the Color ComboBoxColumn for each row still has Red, Blue & Green as its items that can be chosen, but for Row1, "Red" needs to be the selected item and for Row 2, "Green" needs to be the selected item. Keeping with that same idea, the Size ComboBox still has Small, Medium and Large as its items that can be selected for each row, but for Row 1, "Medium" needs to be set as the selected item and for Row 2, "Large" needs to be the selected item.
My DataGridView would be populated the exact same way as it did in January but the selected items that show would be the data that the user had chosen in January. Because I'm retrieving that data (what the user chose in Jan) from the database, I know that in the DataGridView cell for Row1, Color ComboBox, the selected item should be "Red". Since I know that, how do I set the selected item in that ComboBox to "Red"? If this was a straight ComboBox, I know how to set the SelectedItem (or SelectedIndex). But because this is a DataGridViewComboBox, it doesn't seem to work the same way.
Any ideas or suggestions will be greatly appreciated. If more information is needed in order to help me solve this, please let me know.
Thanks in advance.
Ron
|
|
|
|
|
Assume there are two classes: Zoo and Animals. Something like this:
public class Zoo{
public int Id;
public string Name;
public List< Animals > animals;
}
public class Animals{
public int Id;
public string Name;
}
Now, I have two generic lists:
1. List of Zoo
2. List of Animals
3. Any kind of collection (I cannot decide which one to use) which will have Zoo.Id and Animal.Id. This collection would be used as a map between Zoo and Animals. Here each Zoo will have multiple animals and same animal can be a part of multiple zoos.
I need to get the list of Animals in a Zoo and then set that value to Zoo.animals list. I am thinking to use LINQ. Something like:
foreach(Zoo zoo in zooList){
zoo.animals = (from animal in animalList
where collectionThree[animal.Id] = zoo.Id
select animal).ToList();
}
But since collectionThree cannot be a Dictionary/HashTable, this wont work. Any ideas how to do this?modified on Wednesday, March 10, 2010 10:56 PM
|
|
|
|
|
Union statement in SQL Server can be useful.
|
|
|
|
|
Or SortedList. Ex:
public SortedList<Animal, Zoo> map = new SortedList<Animal, Zoo>();
...
...
...
zoo.animals = (from animal in animalList
where map[animal].Id == zoo.Id
select animal).ToList();
}
or
public SortedList<int, int> map = new SortedList<int, int>();
...
...
...
zoo.animals = (from animal in animalList
where map[animal.Id] == zoo.Id
select animal).ToList(); modified on Wednesday, March 10, 2010 2:25 PM
|
|
|
|
|
Thanks!
SortedList is what I was looking for.
|
|
|
|
|
Your 3rd collection is necessary, in fact it breaks the object encapsulation IMO.
What you really need is a List<Zoo> property on Animal , and conversely a List<Animal> property on Zoo . All you need do is ensure that the lists are not accessible publicly, and methods are provided for addition / removal to both list properties "simultaneously" (i.e. adding an Animal to a zoo also adds that zoo to an animal). You will also need properties or methods for getting copies (so the originals are unalterable externally) of the lists.Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
|
|
|
|