Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Underlined phrases gave that error "GridControl1 is a field but used like a type"

How can i solve this?



public DataView Data        
        {
            get
            {
                Excel.Application excelApp = new Excel.Application();
                Excel.Workbook workbook;
                Excel.Worksheet worksheet;
                Excel.Range range;
                workbook = excelApp.Workbooks.Open(Environment.CurrentDirectory + "\\Excel.xlsx");
                worksheet = (Excel.Worksheet)workbook.Sheets["Test Sheet"];//.get_Item(1);

                int column = 0;
                int row = 0;
                GridControl1 Gd = new GridControl1(); 

                range = worksheet.UsedRange;

                GridControl1 Gd = new GridControl1(); 
                
                Gd.Columns.Add("ID");
                Gd.Columns.Add("Name");
                Gd.Columns.Add("Position");
                Gd.Columns.Add("Web Site");
                
                for (row = 2; row <= range.Rows.Count; row++)
                {
                 DataRow dr = Gd.NewRow();
                 for (column = 1; column <= range.Columns.Count; column++)
                    {
                 dr[column - 1] = (range.Cells[row, column] as Excel.Range).Value2.ToString();
                    }
                    Gd.Rows.Add(dr);
                    Gd.AcceptChanges();
                }
                workbook.Close(true, System.Type.Missing, System.Type.Missing);
                excelApp.Quit();
                return Gd.DefaultView;
Posted

1 solution

You have a mistake in your code. I guess there is a GridControl control on the page with ID 'GridControl1' and you are creating the instance of its 'ID" with this code-And that too, two times in the same scope...
C#
GridControl1 Gd = new GridControl1();

              range = worksheet.UsedRange;

              GridControl1 Gd = new GridControl1();


Rather you should create a instance of Gridview control like this -
C#
GridControl Gd = new GridControl();
              range = worksheet.UsedRange;
 
Share this answer
 
Comments
Hslldm 9-Feb-13 9:02am    
Then for them;
Gd.Columns.Add("ID");
Gd.Columns.Add("Name");
Gd.Columns.Add("Position");
Gd.Columns.Add("Web Site");

it gives "..has some invalid arguments" error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900