Computers/Language 2012. 9. 6. 23:26

C# - 엑셀과 데이터 그리드 : Excel & DataFridView



                DataSet dataSetMonthly = null;  일단 널로 선언해 놓고

                if (System.IO.File.Exists(MonthlyFile))  // No  BUT use header from file

                {

                    conn.Open();

              // leave it as 'null' until reading real excel file

                    dataSetMonthly = new DataSet();       // 사용할 떄 할당

                    dataSetMonthly.Clear();

                    adaptorMaster.SelectCommand = cmd;

                    adaptorMaster.Fill(dataSetMonthly);

                    dGViewFuelingLog.DataSource = dataSetMonthly.Tables[0];

                    FormatFuelingLogDataGrid();

                }


새로 화일을 만들어 데이터 그리드뷰에 뿌리고 새로운 데이터 추가

            if (dataSetMonthly == null)    //  새로만든건지 불러들인건지 파악하는데 사용

            {

                dGViewFuelingLog.Rows.Add();

            }

엑셀에서 읽어 데이터 그리드뷰에 뿌리고 데이터 추가

            else      //  read from existing excel file

            {

                dataSetMonthly.Tables[0].Rows.Add();

                dGViewFuelingLog.DataSource = dataSetMonthly.Tables[0];

                FormatFuelingLogDataGrid();

            }

이후 데이터 그리드에 새 데이터 열 추가


            for (int i = 0; i < dGViewFuelingLog.Columns.Count; i++)

            {

                dGViewFuelingLog.Rows[dGViewFuelingLog.Rows.Count - 2].Cells[i].Value

                         = strFuleLogMonth[i];

            }

posted by 털보네i
: