Solved

datagrid problem

Posted on 2004-09-28
5
313 Views
Last Modified: 2010-08-05
hello. I have found understanding how the VB.NET c# datagrids are supposed to be used to be hard work.

But they are obviously powerful. I have 2 questions.

1) (EASY) Can you tell me how to disable the "edittable" nature of the datagrid and set it so that if a cell is double clicked it doesn't activate it (for want of a beter word) for editting? i am using a mySql Datbase and I maually read with sql into a query and I don't want to have the illusion the datagrid can be editted - it can't - to do that you have to double click a row to get a edit box.

2) (HARD)

here is the difficult bit... I want to loop through the datagrid and check if the row is selected and collect the "id" column value into an array. the grid works great and the below code works fine UNTIL the user decides to alter the datagrid order (say to "ASCending on date") by clicking on the column header.

What does this do? well the by clicking to resort the data only the data posted to the screen gets physically resorted, the following code gets all messed up - it WRONGLY IGNORES the new order and continually posts OLD column values. A reordered datagrid should show the data of the new order. But it doesn't ( so its effecivley gibberish to re-order it) i.e. the new order the user generated has not been adopted by the datagrid really - the "selected rows" still pertain to the original sort order only.

private void button1_Click(object sender, System.EventArgs e)
{
   int i;
   for (i=0;i<Contacts.Rows.Count;i++)
   {                        
      if(grdOutput.IsSelected(i))
      {
         MessageBox.Show("row:" + Contacts.Rows[i]["id"] + "");
      }
   }
}

obviously I am diong something wrong. Either I need to "refresh" the datatable/datagrid so it realises its been reordered by the user or I have to have a better piece of code than the "BODGE JOB" I have written above. I am obviously NOT doing this right.. can you help..?
            
0
Comment
Question by:conan1975
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 1

Assisted Solution

by:ryeandi
ryeandi earned 150 total points
ID: 12171406
hi conan,

1)  if you still want to be able to select items in the grid (which you obviously do) you could do something like this:

//*****************************************************************
//Create a table style
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "DataTableName";
      
//Add the columns you will be displaying
DataGridTextBoxColumn dgcs = new DataGridTextBoxColumn();
dgcs.MappingName = "ID";
dgcs.Width = 60;
dgts.GridColumnStyles.Add(dgcs);

dgcs = new DataGridTextBoxColumn();


i'll look at part 2 and get back to you if no one has already.


rye
dgcs.MappingName = "Value";
dgcs.Width = 80;
dgts.GridColumnStyles.Add(dgcs);

//Add the table style to the DataGrid
dg.TableStyles.Add(dg);  //dg = DataGrid

//Set the column st
//The GridColumnStyle[index] is in the order in which you added above
DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dg.TableStyles[0].GridColumnStyles[0];
dgtb.TextBox.Enabled = false;
//*******************************************************************

if you didn't need to select you could just set dg.enabled to false.
0
 
LVL 1

Expert Comment

by:ryeandi
ID: 12171443
sorry, i signed my farewell in the middle of the post for some reason.  
0
 
LVL 5

Accepted Solution

by:
tomasX2 earned 350 total points
ID: 12172800
once you sort the datagrid there is no longer any relationship between the index of the datagrid and datatable...
haven´t compiled because i don´t have the Ide infront of me...

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q934q

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q775q

private void button1_Click(object sender, System.EventArgs e)
{
    ArrayList al = new ArrayList();
     CurrencyManager cm = (CurrencyManager)this.BindingContext[dg.DataSource, dg.DataMember];
     DataView dv = (DataView)cm.List;
     for(int i = 0; i < dv.Count; ++i)
     {
          if(grdOutput.IsSelected(i))
                MessageBox.Show("row:" + dv[i]["id"] + "");
     }
 
     return al;  

   }
}
0
 

Author Comment

by:conan1975
ID: 12179100
tomasX2

thanks very much it works perfectly.... cheers mate

ryeandi
I am sure its my fault but I can't get your code to work and I think I need to understand bound data properly. can I give you 150 points and Tomas 350?
0
 
LVL 1

Expert Comment

by:ryeandi
ID: 12180474
you will want to set your TableStyle.MappingName equal to that of the name of the DataSource for the DataGrid.  so if you are using a DataTable make sure the names are the same:

DataTable myDT = New DataTable("myTableName");

DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "myTableName";


i don't know if it is also necessary to name the columns of the column styles the same as the columns of the data source, but i always do for consistency.  so if myDT has columns {"col1","col2",..ect}

i'd do:

DataGridTextBoxColumn dgcs = new DataGridTextBoxColumn();
dgcs.MappingName = "col1";
dgcs.Width = 60;
dgts.GridColumnStyles.Add(dgcs);


let me know if this works for you.




0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question