Solved

datagrid problem

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
scanning dentists xray (the small ones) 3 53
What is "public object" in C#? 3 45
compare between entities in entity framework 8 70
Service Controller 1 23
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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