1
2
3
4
5
6
7
8
9
10
11
//현재 셀 값을 표시
datagridviewA.CurrentCell.Value;
 
//현재 셀의 열 인덱스를 표시
datagridviewA.CurrentCell.ColumnIndex;
 
//현재 셀의 행 인덱스를 표시
datagridviewA.CurrentCell.RowIndex;
 
//  datagridviewA.currentRow, 등등 여러가지로 응용할 수 있다
//(0, 0)에 현재셀을 위치하고 싶을때 DataGridView1.CurrentCell = DataGridView1[0, 0];
cs


'Winform' 카테고리의 다른 글

winform 열 숨기기  (0) 2019.01.30
winform 헤더, 셀 값 가운데로 정렬  (0) 2019.01.30
winform 각 행의 색표시  (0) 2019.01.30
winform 차트 예제  (0) 2019.01.30
winform 엑셀 저장(초고속)  (0) 2019.01.29

데이터 그리드 뷰 열을 숨겨야 할 때가 있는데 한번 보겠습니다


1
2
3
4
//CustomerID column 숨기기
//원래 값은 true
//this 안써줘도 됩니다

this.dataGridView1.Columns["CustomerID"].Visible = false;
cs


'Winform' 카테고리의 다른 글

winform 현제 셀값 가져오기(위치포함)  (0) 2019.01.30
winform 헤더, 셀 값 가운데로 정렬  (0) 2019.01.30
winform 각 행의 색표시  (0) 2019.01.30
winform 차트 예제  (0) 2019.01.30
winform 엑셀 저장(초고속)  (0) 2019.01.29

헤더와 셀 값을 가운데로 정렬해보자


1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
 
            // 헤더 값을 가운데로 정렬
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;            
            
            // 셀 값을 가운데로 정렬
            dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
 
            // 0번째 컬럼값 전체 가운데로 정렬
            dataGridView1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
 
            // Column1의 전체 셀값 가운데로 정렬
            dataGridView1.Columns["Column1"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
 
cs


'Winform' 카테고리의 다른 글

winform 현제 셀값 가져오기(위치포함)  (0) 2019.01.30
winform 열 숨기기  (0) 2019.01.30
winform 각 행의 색표시  (0) 2019.01.30
winform 차트 예제  (0) 2019.01.30
winform 엑셀 저장(초고속)  (0) 2019.01.29

윈폼 사용할때 보면 데이터그리드뷰가 밋밋할 때가 있다 보기도 좋지않고...


그럴때 각 행마다 색을 다르게 하면 좀더 이쁘고 가독성도 좋다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//짝수 홀수 열 색지정
// rows 대신 column을 쓰면 각 column 색이 바꿔집니다 ㅎㅎ
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
       if (i % 2 != 0)
         {
             dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.FromArgb(240255240);
         }
         else
         {
              dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.FromArgb(240255255);
         }
     }

//짝수 홀수 열 색지정(한줄)

dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua;

cs


'Winform' 카테고리의 다른 글

winform 열 숨기기  (0) 2019.01.30
winform 헤더, 셀 값 가운데로 정렬  (0) 2019.01.30
winform 차트 예제  (0) 2019.01.30
winform 엑셀 저장(초고속)  (0) 2019.01.29
winform 엑셀 죽이기 완벽한 방법  (0) 2019.01.29

윈폼에 있는 차트 예제를 정리해 보았다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
                    //차트 해보자 테스트
 
                    //차트 시리즈1 삭제(초기화)
                    chart1.Series.Clear();
 
                    //시리즈 추가
                    //(각각을 시리즈라하고 Series[0], Series[1]로 표시할 수도 있다
                    chart1.Series.Add("LG");
                    chart1.Series.Add("SPEC");
 
                    //legend 위치
                    //LG, SPEC 두개 시리즈를 합쳐서 하나의 Legends라고 한다 마찬가지로 [0]번지 표시 가능
                    chart1.Legends[0].Alignment = StringAlignment.Center;
                    chart1.Legends[0].Docking = Docking.Bottom;
                    chart2.Legends[0].Alignment = StringAlignment.Center;
                    chart2.Legends[0].Docking = Docking.Bottom;
 
                    //색지정, 스타일 지정
                    // 원래 디폴트 값은 막대이지만 선이나 점선을 하고 싶을때에는 아래와 같이..
                    chart1.Series["SPEC"].ChartType = SeriesChartType.Line;
                    chart1.Series["SPEC"].Color = Color.Red;
                    chart1.Series["SPEC"].BorderWidth = 3;
                    chart1.Series["SPEC"].BorderDashStyle = ChartDashStyle.Dash;
 
                    //포인츠 초기화
                    chart1.Series["LG"].Points.Clear();
 
                    // 값 넣기
                    // LG 시리즈에 1, 100막대를 넣음 AddY()만 써도 
                    chart1.Series["LG"].Points.AddXY(1,100);
 
 
 
cs


'Winform' 카테고리의 다른 글

winform 헤더, 셀 값 가운데로 정렬  (0) 2019.01.30
winform 각 행의 색표시  (0) 2019.01.30
winform 엑셀 저장(초고속)  (0) 2019.01.29
winform 엑셀 죽이기 완벽한 방법  (0) 2019.01.29
winform DataGridView to Excel  (0) 2019.01.22

winform 엑셀 저장(초고속) 클립보드 캡쳐방식입니다



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
private void Export_Excel(string path) //savefildialog.filename
        {
            //xls는 65536, xlsx는 1048575
            dataGridView1.MultiSelect = true;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AllowUserToAddRows = false;
 
            dataGridView1.SelectAll();
            DataObject dataObj = dataGridView1.GetClipboardContent();
            if (dataObj != null)
            {
                Clipboard.SetDataObject(dataObj);
            }
            Excel.Application xlexcel;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = Missing.Value;
            xlexcel = new Excel.Application();
            xlexcel.Visible = false;
            xlWorkBook = xlexcel.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[11];
            CR.Select();
            xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
            dataGridView1.MultiSelect = false;
 
            if (path.Contains(".xlsx"))
            {
                xlWorkBook.SaveAs(path, Excel.XlFileFormat.xlWorkbookDefault, nullnullfalsefalse,
                Excel.XlSaveAsAccessMode.xlShared, falsefalsenullnullnull);
            }
            else
            {
                xlWorkBook.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal, nullnullfalsefalse,
                Excel.XlSaveAsAccessMode.xlShared, falsefalsenullnullnull);
            }
 
            dataGridView1.ClearSelection();
            xlexcel.Quit();
 
            releaseObject(CR);
            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlexcel);
            MessageBox.Show("저장완료");
        }
cs


'Winform' 카테고리의 다른 글

winform 각 행의 색표시  (0) 2019.01.30
winform 차트 예제  (0) 2019.01.30
winform 엑셀 죽이기 완벽한 방법  (0) 2019.01.29
winform DataGridView to Excel  (0) 2019.01.22
winform 엑셀 import  (0) 2019.01.17

winform 에서 엑셀 작업을 하고 프로세스를 지워주지 않으면 메모리를 계속 갉아 먹기에 컴퓨터가 느려진다


winform 에서 엑셀 작업을 한뒤에는 반드시 프로세스를 지워줘야 하는데  이 지우는 방법이 매우 어렵더라


구글링 해본 결과 엑셀작업을 할때 차례대로 열어놓은 엑샐 객체들을 차례대로 닫고 릴리즈 해줘야 한다는데 


적용이 힘들고 잘 되지 않을 뿐더러 무엇보다더 지저분해진다


나의 방법은 내가 winform 엑샐 작업시 열었던 그 엑셀의 프로세스 ID를 기억해 process.kill(프로세스ID) 해버리는 방법이다


이 방법의 좋은 점은 간편하고 쉬우며 직관성이 좋다.


아래에 코드를 소개 하겠으니 모두들 홧팅



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 프로세스 id 찾는
[DllImport("user32.dll")]
static extern int GetWindowThreadProcessId(int hWnd, out int lpdwProcessId);
 
int GetExcelProcess(Excel.Application excelApp)
    {
        int id;
        GetWindowThreadProcessId(excelApp.Hwnd, out id);
            
        //return Process.GetProcessById(id); 리턴방식으로 써도 됨
        return id;
    }
 
 
// Excel 프로그램을 의미합니다.
Excel.Application excelApp = new Excel.Application();    
 
//엑셀 프로세스를 찾아서~
 Process p = Process.GetProcessById(GetExcelProcess(excelApp));
 
//죽이자
p.Kill();  
cs


'Winform' 카테고리의 다른 글

winform 차트 예제  (0) 2019.01.30
winform 엑셀 저장(초고속)  (0) 2019.01.29
winform DataGridView to Excel  (0) 2019.01.22
winform 엑셀 import  (0) 2019.01.17
winform cursor waiting 마우스 로딩  (0) 2019.01.17

이 방법은 클립보드 형식이라서 매우 빠릅니다


https://stackoverflow.com/questions/18182029/how-to-export-datagridview-data-instantly-to-excel-on-button-click




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//쓰기
        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AllowUserToAddRows = false;
 
            dataGridView1.SelectAll();
            DataObject dataObj = dataGridView1.GetClipboardContent();
            if (dataObj != null)
            {
                Clipboard.SetDataObject(dataObj);
            }
            Excel.Application xlexcel;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = Missing.Value;
            xlexcel = new Excel.Application();
            xlexcel.Visible = true;
            xlWorkBook = xlexcel.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[11];
            CR.Select();
            xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
 
        }
cs





+ Recent posts