Solved

datagrid problem

Posted on 2004-09-28
5
308 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now