|
Metro and WinRT are built on .Net, although the UI comes from Silverlight/WPF which is .Net 3.5 I think. But the language is the same, the core libraries are the same, and learning .Net 3.0 will give you a very good grounding in everything important that you'll need to be able to pick up WPF based approaches.
|
|
|
|
|
Thank you Richard and Bob.
Hope you are both correct.
|
|
|
|
|
Hey guys,
Easy one here. I want to see the results of my datatable in the DataGridView. When i execute, it runs without errors but no DataGridView shows up on screen. Does DataGridView require a form to view the results or something?
What am I missing?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FileHelpers;
using System.Data;
using System.IO;
using System.Data.OleDb;
using System.Windows.Forms;
namespace CSVParser
{
class CSVParser
{
public static DataTable ParseCSV(string path)
{
if (!File.Exists(path))
return null;
string full = Path.GetFullPath(path);
string file = Path.GetFileName(full);
string dir = Path.GetDirectoryName(full);
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=\"" + dir + "\\\";"
+ "Extended Properties=\"text;HDR=No;FMT=Delimited\"";
string query = "SELECT * FROM " + @"C:\Users\rsharp\Desktop\CustomerExport.csv";
DataTable dTable = new DataTable();
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
dAdapter.Fill(dTable);
DataGridView dgView = new DataGridView();
BindingSource bSource = new BindingSource();
bSource.DataSource = dTable;
dgView.DataSource = bSource;
dAdapter.Dispose();
return dTable;
}
}
}
modified 21-Nov-12 19:21pm.
|
|
|
|
|
RickSharp wrote: Doesn DataGridView require a form to view the results or something?
Yes, it does. It needs to be assigned to the Controls collection of the form that it's supposed to show on.
Where's the entry-point of your application?
|
|
|
|
|
By entry point do you mean is it just a win form, exe or class library? If so I have added a form project and console application project to my solution. So I have a class library that contains that code. I added a reference to that class library in the new forms and console projects that I created. I tried following this tutorial with no success.
http://www.dotnetperls.com/datagridview-tutorial[^]
|
|
|
|
|
RickSharp wrote: By entry point do you mean is it just a win form, exe or class library? If so I have added a form project and console application project to my solution. So I have a class library that contains that code. I added a reference to that class library in the new forms and console projects that I created. I tried following this tutorial with no success.
The tutorial has some different code than you have; A console-application doesn't show a form usually. There's also no need for a separate class-library - simply put then in a normal forms-application.
|
|
|
|
|
I have 2 texboxes and 1 button
TextBoxSearch.Text;
TextBoxName.Text;
and 1 button(searchbutton)
The thing i want to do is when i press the Search button and type an id in textboxsearch.text ( so the id will get the data and pass it in my textboxname.text
I have written so long ,
var sr=new ServiceReferens1.Product();
//here i have tryed
// TextBoxSearch.Text=sr.ProductID;
// and many more thing have i tryed so i need help!
|
|
|
|
|
Workflow
1. Enter search criteria in textbox1
2. Click button
3. Find record (details)
4. Display Results in textbox2
In the button click event do the following
1. Get the list of products
2. Use a linq query or a for..next loop to locate the product record base on you criteria
3. Set the located product in a SelectedProduct object (to be used later)
4. Set the SelectedProduct.ProductName into the textbox2
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I have been thinking about this for a while and have not come up with any simple and beautiful ways to solve this problem. What I would like to have done is a nice way to sort Lists along side other Lists. Such as shown in the example:
I have a generic amount of Lists (let us just say 4) and they are independent of each other but are of same length.
listOne = [09,06,04,03]
listTwo = [20,50,40,30]
listThree = [15,45,85,65]
listFour = [89,39,19,99]
Now I would like to organize listOne but also move the indices of listTwo and listThree:
listOne = [03,04,06,09]
listTwo = [30,40,50,20]
listThree = [65,85,45,15]
I then work with these lists as I see fit. Now I would like to organize listFour but move listOne, listTwo and listThree with it:
listFour = [19,39,89,99]
listOne = [06,04,03,09]
listTwo = [50,40,30,20]
listThree = [45,85,65,15]
Is there already a development that does something like this? A way to organize any list and move the indexes of any number of extra lists with the organization of the first list. Right now I use the Dictionary type and have each indexed, then when one is organized, move the others with respect to the new Dictionary index. But that seems to be very sloppy.
|
|
|
|
|
Option 1:
It's not clean, but if you want to sort the lists, this should work:
static void Sort<T>(IList<T> sortKey, params IList<T>[] otherLists)
{
Action<IList<T>, IDictionary<int, int>> sortList = (list, keys) =>
{
var values = list
.Select((value, i) => new { key = keys[i], value })
.OrderBy(p => p.key).Select(p => p.value);
int index = 0;
foreach (T value in values)
{
list[index++] = value;
}
};
var indices = Enumerable.Range(0, sortKey.Count)
.OrderBy(i => sortKey[i])
.Select((key, value) => new { key, value })
.ToDictionary(p => p.key, p => p.value);
sortList(sortKey, indices);
foreach (var list in otherLists)
{
sortList(list, indices);
}
}
Edit:
Missed a ToList call in the anonymous delegate which would prevent this from working. Option 2 is probably a better approach.
Edit 2:
Nope, the ToList call isn't needed - the OrderBy call ensures that the entire sequence is evaluated on the first call to MoveNext .
It still ends up creating a copy of the list, so option 2 is still better.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 22-Nov-12 7:15am.
|
|
|
|
|
Your issue is that you are trying to sort your lists as 4 different lists when they aren't. It's a single list of x items.
So just have:
class SomeObject
{
int Data[4];
}
List<SomeObject> lst;
When you sort, you would provide a custom compare function that'll look like:
int Compare(SomeObject x, SomeObject y)
{
compare x.Data[0] vs. y.Data[0] or whatever
}
and all "columns" will move together.
|
|
|
|
|
Option 2:
Wrap your lists with a class which applies the sort order where necessary:
public sealed class ListHelper<T> : IEnumerable<IList<T>>
{
private int _sortIndex = -1;
private IDictionary<int, int> _indexMap;
private IDictionary<int, int> _reverseIndexMap;
private readonly List<MappedList> _lists = new List<MappedList>();
public int SortIndex
{
get
{
return _sortIndex;
}
set
{
if (value < -1 || value >= _lists.Count)
{
throw new ArgumentOutOfRangeException();
}
if (-1 == value)
{
if (0 != _lists.Count)
{
int size = _lists[0].Count;
_indexMap = Enumerable.Range(0, size).ToDictionary(i => i);
_reverseIndexMap = _indexMap;
}
}
else
{
IList<T> sortKey = _lists[value].InnerList;
_indexMap = Enumerable.Range(0, sortKey.Count)
.OrderBy(i => sortKey[i])
.Select((key, v) => new { key, value = v })
.ToDictionary(p => p.key, p => p.value);
_reverseIndexMap = _indexMap.ToDictionary(p => p.Value, p => p.Key);
}
}
}
public int Count
{
get { return _lists.Count; }
}
public IList<T> this[int index]
{
get { return _lists[index]; }
}
public void Add(IList<T> listToAdd)
{
if (_indexMap == null)
{
_indexMap = Enumerable.Range(0, listToAdd.Count).ToDictionary(i => i);
_reverseIndexMap = _indexMap;
}
else if (_indexMap.Count != listToAdd.Count)
{
throw new ArgumentException("Invalid list size!", "list");
}
_lists.Add(new MappedList(listToAdd, this));
}
public IEnumerator<IList<T>> GetEnumerator()
{
return _lists.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private sealed class MappedList : IList<T>
{
private readonly IList<T> _list;
private readonly ListHelper<T> _parent;
public MappedList(IList<T> list, ListHelper<T> parent)
{
_list = list;
_parent = parent;;
}
public IList<T> InnerList
{
get { return _list; }
}
public T this[int index]
{
get
{
int realIndex = _parent._reverseIndexMap[index];
return _list[realIndex];
}
set
{
int realIndex = _parent._reverseIndexMap[index];
_list[realIndex] = value;
}
}
public int Count
{
get { return _list.Count; }
}
public bool IsReadOnly
{
get { return _list.IsReadOnly; }
}
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < _list.Count; i++)
{
int index = _parent._reverseIndexMap[i];
yield return _list[index];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int IndexOf(T item)
{
int result = _list.IndexOf(item);
return -1 == result ? -1 : _parent._indexMap[result];
}
public void CopyTo(T[] array, int arrayIndex)
{
foreach (T item in this)
{
array[arrayIndex++] = item;
}
}
public bool Contains(T item)
{
return _list.Contains(item);
}
public void Add(T item)
{
throw new NotSupportedException();
}
public void Insert(int index, T item)
{
throw new NotSupportedException();
}
public bool Remove(T item)
{
throw new NotSupportedException();
}
public void RemoveAt(int index)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
}
}
}
...
var helper = new ListHelper<int>
{
new[] { 04, 09, 03, 06 },
new[] { 20, 50, 40, 30 },
new[] { 15, 45, 85, 65 },
new[] { 89, 39, 19, 99 },
};
helper.SortIndex = 0;
foreach (IList<T> list in helper)
{
for (int index = 0; index < list.Count; index++)
{
Console.WriteLine(list[index]);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Perhaps have a List of Lists, where the first sub-list is the list of all the first values, etc.:
{
{ 09 , 20 , 15 , 89 }
{ 06 , 50 , 45 , 39 }
{ 04 , 40 , 85 , 19 }
{ 03 , 30 , 65 , 99 }
}
Then sort the outer list by the first (or whatever) element of each:
{
{ 03 , 30 , 65 , 99 }
{ 04 , 40 , 85 , 19 }
{ 06 , 50 , 45 , 39 }
{ 09 , 20 , 15 , 89 }
}
P.S. Could have made this a Friday Programming Challenge.
modified 21-Nov-12 15:14pm.
|
|
|
|
|
Make a wrapper class that you can use to preserve the order:
class OrderHelper<T> : IComparable<OrderHelper<T>> {
public T Item {get; private set;}
public int Order {get; private set;}
public OrderHelper(T item, int order) { this.Item = item; this.Order = order; }
public int CompareTo(OrderHelper<T> other) {
return Comparer<T>.Default.Compare(Item, other.Item);
}
}
Create a list of these from your primary sort list:
int index = 0;
List<OrderHelper<int>> orderedList = listOne.Select(i => new OrderHelper(i, index++)).ToList();
Sort that list, and then you can retrieve the index list:
orderedList.Sort();
List<int> indices = orderedList.Select(oi => oi.Order).ToList();
Finally you can order the other lists by that index set:
public static List<T> OrderBy(List<T> list, List<int> indices){
List<T> r = new List<T>(indices.Count);
for(int i = 0; i < r.Count; i++){
r[i] = list[indices[i]];
}
return r;
}
listTwo = OrderBy(listTwo, indicies);
listThree = OrderBy(listThree, indicies);
|
|
|
|
|
Hi,
I am learning OOP and trying to apply it to my C# WinForms applications.
So I have a Form with 2 textboxes. One to enter professor. Another to enter lecturer.
I have created an abstract 'Staff' class. Then I have a Professor class and a Lecturer class. Both inherit from Staff.
In Staff, I have declared an abstract property StaffName. So this must be implemented in other classes. Am I correct so far?
These classes are called on Button_Click event...
Professor professor = new Professor();
professor.StaffName = staffTextBox1.Text;
Lecturer lecturer = new Lecturer();
lecturer.StaffName = staffTextBox2.Text;
MessageBox.Show(string.Format("Analyst {0}\nSupervisor {1}", professor.StaffName, lecturer.StaffName), "Output", MessageBoxButtons.OK);
And my classes...
public abstract class Staff
{
protected string staffName;
public abstract string StaffName
{
get;
set;
}
}
public class Professor : Staff
{
public override string StaffName
{
get
{
return staffName;
}
set
{
staffName = value;
}
}
}
public class Lecturer : Staff
{
public override string StaffName
{
get
{
return staffName;
}
set
{
staffName = value;
}
}
}
Is this a correct OOP technique in WinForms or is there issues with my code design?
Thanks for your help!!
|
|
|
|
|
You should implement your StaffName property in your base (not abstract) class, as it will be used by all types. Any common properties or methods should be in the base class, and the child classes only need things that are unique to them. So you would have:
public class Staff
{
private string staffName;
public string StaffName
{
get
{
return staffName;
}
set
{
staffName = value;
}
}
}
public class Professor : Staff
{
}
public class Lecturer : Staff
{
}
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Not sure if the OP noticed but I wanted to point this out. In Richard's response, he changed the access modifier for "string staffName" to "private" rather than "protected". Since the "StaffName" property is handled entirely by the base class, the derived types do not need to see the "staffName" member. "private" means that the object is only accessible to the class or struct defining it. "protected" would mean that the derived types could see the member as well.
EDIT:
Richard, I have a question about this now. Haha. What if the programmer decided to create a new class deriving from 'Staff' and the new class needed its own implementation of 'StaffName'? Would it be better to create a new 'private' field in the derived class? Or should 'private staffName' be changed to 'protected' in the base class? (I'm guessing create its own in the new class because none of the other derived types need it. But I wanted to clarify.)
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
Matt U. wrote: In Richard's response, he changed the access modifier for "string staffName" to "private" rather than "protected". Exactly so, because the variable should only be accessible to the getter and setter in the base class; no other class needs to access it. In later versions of .NET you do not even need to define the variable as the compiler generates a hidden one automatically.
Matt U. wrote: What if the new class needed its own implementation of 'StaffName'? Why would it, it's only a name?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Perhaps that was a bad example. Or perhaps you see what I was getting at but it isn't really ever going to happen the way I was talking?
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
Matt U. wrote: Perhaps that was a bad example. No, it was quite reasonable, but you need to understand the use and abuse of properties. They have a specific purpose and the important point is that the actual variable is always hidden from the outside world. If you need to access the variable from another class then you are breaking the encapsulation, and potentially, your program.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
That makes sense. Thanks for the clarification. I guess it's time I study OOP a bit more. Haha.
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
Right. Perhaps a virtual property, but it doesn't sound like a good idea in this case. (I didn't even know properties could be virtual. Why?)
|
|
|
|
|
I don't see what that has to do with WinForms.
I agree with not making StaffName abstract, and using an auto-implemented property. And why name it StaffName rather than Name? And so far I see no reason for inheritance.
So, on the whole, I'd say, "no, it's not a good design (so far)", but that's just me.
Additionally, I'd suggest providing the name and such to the constructor -- an instance should be meaningful once it's instantiated, and a nameless Staff doesn't seem meaningful.
|
|
|
|
|
1 )Which part of your code do you add to your article
2) how much code should you add
3) how important are illustration
|
|
|
|
|
1) Anything required to build the demo -- you included a demo, right?
2) In the article with explanations? More is better.
3) Generally not very, but it depends on the subject. Please don't include screenshots of Visual Studio.
There is also an Article Writing forum here http://www.codeproject.com/Forums/1641/Article-Writing.aspx[^]
|
|
|
|
|