Solved

datagrid problem

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

22 Experts available now in Live!

Get 1:1 Help Now