Windows DataGrid

  • 15 years ago

    Can you use the DataGrid control for non-bound data? Eg. Use it like you would a ListBox and add items?

  • 15 years ago

    i dont think so no, but it does support some nice data sources like arrays, heres what sources it can have.


    A DataTable
    A DataView
    A DataSet
    A DataViewManager
    A single dimension array
    Any component that implements the IListSource interface
    Any component that implements the IList interface

  • 15 years ago

    Okay maybe one of them might be just as good. I just want to display 4 fields that come from a text file and allow the user to edit them (add and edit record like things) and save it back. Using a 1D array might be the easiest, i don't really need the power of the whole IListSource interface.


    I am going to have a look for some code, do you have any on you or know where any is?

  • 15 years ago

    I got it working using an ArrayList implementing an IList and a class for the record.


    Thanks for all your help, I don't think I have anymore questions, but i will ask them if i do.

  • 15 years ago
    All the columns that are shown are in A-Z order because an IList doesn't have an order like a DB table. How can i set an order or choose what columns i want, i want them all so it doesn't matter which way it is done. I tried playing around with the DataGridTableStyle but i cannot get it to do anything.
  • 15 years ago
    have you got the code you use to input from file, to array to grid? a simple way would be input the data from file to array in correct column order first. yeah msdn says DataGridTableStyle can control column ordering but cant see much at the mo, ill keep looking.
  • 15 years ago

    Here is the code that opens the file and loads it into the Array. (it is the next two blocks of code.


    Code:
       Private CurrentFileName As String
       Private CustomerList As ArrayList
       Private cCustomer As Customer '// Current Customer to be stored


    Code:
                   '// Reset CustomerList
                   CustomerList = New ArrayList


                   CurrentFileName = OpenDialog.FileName
                   Me.Text = Application.ProductName & " - [" & CurrentFileName & "]"



                   Dim oFileStream As New FileStream(CurrentFileName, FileMode.Open, FileAccess.Read)
                   Dim oStreamReader As New StreamReader(oFileStream)
                   oStreamReader.BaseStream.Seek(0, SeekOrigin.Begin)


                   '// Read pass the number of customers line
                   oStreamReader.ReadLine()


                   Dim iCurrentCustomer As Integer = 1


                   While oStreamReader.Peek() > -1
                       '// Peek method of StreamReader object tells how much more data is left in the file


                       cCustomer = New Customer
                       With cCustomer
                           .Number = oStreamReader.ReadLine
                           .PIN = oStreamReader.ReadLine
                           .CheckingBalance = oStreamReader.ReadLine
                           .SavingsBalance = oStreamReader.ReadLine
                       End With
                       CustomerList.Add(cCustomer)


                       iCurrentCustomer += 1
                   End While
                   oStreamReader.Close()


                   With dGridCustomers
                       .DataSource = Nothing
                       .DataSource = CustomerList
                       .Refresh()
                   End With



    Here is the code i found for DataGridTableStyle. The columns are in order in the file and are read in order but once they are put in an instance of the Customer class they are properties. I cannot get it to work, the last few lines of code don't even relate.


    Code:
           Dim TableStyle As New DataGridTableStyle
           Dim DicGridColumns As GridColumnStylesCollection = TableStyle.GridColumnStyles


           ' Define columns
           With DicGridColumns
               .Add(New DataGridTextBoxColumn)
               With .Item(0)
                   .MappingName = "Number"
                   .HeaderText = "Customer Number"
                   .Width = 40
                   .NullText = String.Empty
               End With
               .Add(New DataGridTextBoxColumn)
               With .Item(1)
                   .MappingName = "PIN"
                   .HeaderText = "PIN"
                   .Width = 60
                   .NullText = String.Empty
               End With
               .Add(New DataGridTextBoxColumn)
               With .Item(2)
                   .MappingName = "CheckingBalance"
                   .HeaderText = "Checking Balance"
                   .Width = 120
                   .NullText = String.Empty
               End With
               .Add(New DataGridTextBoxColumn)
               With .Item(3)
                   .MappingName = "SavingsBalance"
                   .HeaderText = "Savings Balance"
                   .Width = 50
                   .NullText = String.Empty
               End With
           End With
           ' Define Dictionary Grid Columns Styles
           dGridCustomers.TableStyles.Add(TableStyle)
           ModelColumn.combo.DictionaryGridColumns = DicGridColumns
           ' Define Dictionary Grid Height
           ModelColumn.combo.DictionaryGridHeight = 250
           ' Define Dictionary Grid Width
           ModelColumn.combo.DictionaryGridWidth = 350

  • 15 years ago
    Colin: I was hoping on a reply, but i guess you didn't find anything useful.
  • 15 years ago
    hi, sorry i had a quick look but didnt code anything, ive tried the arraylist way this morning and it came out in different orders, so i tried a different method, data into a normal array, then into datset then dataset in datagrid, it works and in order so in my text file i had

    Num99999
    Pin00000
    Check444
    SAVING00

    so i wanted it to display in the datagrid like that i did (note i only have c# 2003,converter on this site as you know)

    Code:

       //LOAD FILE INTO ARRAY
       StreamReader r = new StreamReader(Application.StartupPath + "\\Data.txt");
       string[] arr = new string[4];
       arr.SetValue(r.ReadLine(),0);
       arr.SetValue(r.ReadLine(),1);
       arr.SetValue(r.ReadLine(),2);
       arr.SetValue(r.ReadLine(),3);

       //CREATE NEW DATASET + ADD ROWS
       DataSet CustomerList = new DataSet("DataS");
       CustomerList.Tables.Add("New");
       DataRow dr = CustomerList.Tables[0].NewRow();
       DataColumn dc = new DataColumn();
       dc.ColumnName = "Col 0";
       DataColumn dc1 = new DataColumn();
       dc1.ColumnName = "Col 1";
       DataColumn dc2 = new DataColumn();
       dc2.ColumnName = "Col 2";
       DataColumn dc3 = new DataColumn();
       dc3.ColumnName = "Col 3";

       CustomerList.Tables[0].Columns.Add(dc);
       CustomerList.Tables[0].Columns.Add(dc1);
       CustomerList.Tables[0].Columns.Add(dc2);
       CustomerList.Tables[0].Columns.Add(dc3);
       dr.ItemArray = arr;
       CustomerList.Tables["New"].Rows.Add(dr);;

       //NOW ADD TO DATAGRID
       dataGrid1.DataSource = CustomerList;
       dataGrid1.Expand(-1);


    i know it wasnt the first way around the problem but it does work, hope it helps
  • 15 years ago
    Thanks for that, it looks like it works fine. Before i go and attempt to implement it can you easily delete a row in the dataset without having binding problems, really it needs to do it without unbinding it.
  • 15 years ago

    sods law the second i close vs down, you reply  , well i presume you will have to rebind it if you delete a row, ill try...

  • 15 years ago

    Thanks, because i had problems when i deleted the last record and rebinded it, the datagrid would give me an error when i clicked a record after deleting it.

  • 15 years ago

    Just tried it, i removed a row in the dataset .RemoveAt(1) and it worked fine, ive updated the code slightly tho so i made the dataset public, new code, also i didnt rebind it, i just refreshed the datagrid which worked.


    Code:

    public DataSet CustomerList;
    private void btnLoad_Click(object sender, System.EventArgs e)
    {
       //LOAD FILE INTO ARRAY
       StreamReader r = new StreamReader(Application.StartupPath + "\Data.txt");
       string[] arr = new string[4];
       arr.SetValue(r.ReadLine(),0);
       arr.SetValue(r.ReadLine(),1);
       arr.SetValue(r.ReadLine(),2);
       arr.SetValue(r.ReadLine(),3);


       //CREATE NEW DATASET + ADD ROWS
       CustomerList = new DataSet("DataS");
       CustomerList.Tables.Add("New");


       //ADD COLUMNS
       DataColumn dc = new DataColumn();
       dc.ColumnName = "Col 0";
       DataColumn dc1 = new DataColumn();
       dc1.ColumnName = "Col 1";
       DataColumn dc2 = new DataColumn();
       dc2.ColumnName = "Col 2";
       DataColumn dc3 = new DataColumn();
       dc3.ColumnName = "Col 3";


       CustomerList.Tables[0].Columns.Add(dc);
       CustomerList.Tables[0].Columns.Add(dc1);
       CustomerList.Tables[0].Columns.Add(dc2);
       CustomerList.Tables[0].Columns.Add(dc3);


       //ADD ROWS
       DataRow dr = CustomerList.Tables[0].NewRow();
       DataRow dr1 = CustomerList.Tables[0].NewRow();
       DataRow dr2 = CustomerList.Tables[0].NewRow();
       dr.ItemArray = arr;
       dr1.ItemArray = arr;
       dr2.ItemArray = arr;


       CustomerList.Tables[0].Rows.Add(dr);;
       CustomerList.Tables[0].Rows.Add(dr1);;
       CustomerList.Tables[0].Rows.Add(dr2);;
                   
       //NOW ADD TO DATAGRID
       dataGrid1.DataSource = CustomerList;
       dataGrid1.Expand(-1);
    }


    private void button1_Click(object sender, System.EventArgs e)
    {
       CustomerList.Tables[0].Rows.RemoveAt(1);
       dataGrid1.Refresh();
    }



  • 15 years ago

    Because you actually made a project and are testing this could you send the project over so that i can easily see what you have done without me having to build it all up from scratch. I have C# but i started this program in VB.NET so it stays there. Just use the email i have in Developer Fusion.


    I have been learning Java recently and in VB.NET i keep typing // to write a comment because i am now so use of it and it seems strange going to VB.NET because the whole syntax is different.


    Thanks for all your help.

  • 15 years ago

    i should point out tho, when using the dataset you are putting more resources into memory, ok so its not a huge amount of data but if it gets 1000s of records it would be better using a database than flat file and a datareader. it depends totally on the system your deploying it on and its use

  • 15 years ago

    it is out of my outbox now, chugging its way to you now


    sent to [email protected]


    i havnt tried java, is it worth it?? advantages or what can you do with it?

  • 15 years ago

    I am aware of that, I am building an online game server and that is using a database (nope not an Access DB)


    I would be very surprised if someone wanted more than 25 records in the file.


    If you didn't have to rebind then it should be fine if you remove the last item in the list. Could you send me the code before i put it in because i don't have much time tonight to put it in the program because i am still doing a uni assignment that is due on Friday but i want to get it done so i can work on other assignments.


    Thanks heaps for your time

  • 15 years ago

    Thanks for that. I finally got your email. i'll try it and get back to you.


    Java is pretty cool, i can see how people claim that Microsoft stole the design of .NET from Java. They are very very similar, even heaps of the library namespaces are the same.

  • 15 years ago

    How do you get rid of the "New" thing and also the two icons in the top right corner?


    I noticed i didn't say much about Java. I think that J2EE sounds really good but i don't have the time to learn JavaBeans and the likes. For windows programs .NET is heaps better, by windows i mean programs with controls. I haven't found any good IDEs for Java that can make forms like Visual Studio because AWT and Swing which are Java's GUI classes are very complex. But i am sure there would be some out there.

  • 15 years ago

    sorry didnt reply, was having bath, thought of it in the bath tho, when you bind the datasource , only bind the table like


    dataGrid1.DataSource = CustomerList.Tables[0];

  • 15 years ago

    thats cool, sweet thats works just like i had it before, i just have to make it read in the file like i had before and it will all be the same.


    Thanks heaps. If i have anymore problems i'll let you know in the next few days. Been spending and will be spending quite a bit of time on uni assignments. Thanks again for your help.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“An idiot with a computer is a faster, better idiot” - Rich Julius