2011-08-24 6 views
0

클릭하면 을 클릭하는 방법을 만들었습니다. 파일 찾아보기 openDialogBox가 나타납니다. 그러나 그렇지 않습니다. 뭐가 잘못 되었 니 ?콤보 상자를 선택하면 openFileDialog가 나타나야합니다.

public void Lista() 
     { 
      string[] col2 = new string[dataGridView1.Rows.Count]; 

      for (int i = 0; i < dataGridView1.Rows.Count; i++) 

        if (col2[i] == "Browse From File...") 
        { 
         DialogResult result2 = openFileDialog2.ShowDialog(); 
         if (result2 == DialogResult.OK) 
         { 
          // filename = openFileDialog1.FileName; 
         } 
     } 
     } 

이 나는 ​​방법 챠트를 호출하는 방법입니다 :

enter image description here

내가 OpenFileDialog를 열기 만든 방법이다.

private void button1_Click(object sender, EventArgs e) 
      { 
       // opens window **BROWSE** 

       openFileDialog1.Title = "Choose File CSV "; 

       string filename = ""; 
       DialogResult result = openFileDialog1.ShowDialog(); 
       if (result == DialogResult.OK) 
       { 
        filename = openFileDialog1.FileName; 

        textBox1.Text = filename; 






        string line; 
        // Read the file and display it line by line. 


        System.IO.StreamReader file = new System.IO.StreamReader(textBox1.Text); 

        stringforData = file.ReadLine();  
        while ((line = file.ReadLine()) != null) 
        { 
         //read inside the table 
         fileList.Add(line.Split(';')); 
        } 

        file.Close(); 



        this.ToDataGrid(); 
        this.Lista(); 
       } 
      } 
+0

당신이 얼마나 멀리 볼 수 있도록 단계별 해봤 문제가 해결됩니다? 그것은 openFileDialog2.ShowDialog()를 시도하고 대화 상자가 나타나지 않습니까? Lista가 값을 반복하고 텍스트를 확인하기 때문에 어떤 일이 일어나고 있는지 혼란스러워합니다. 나는 그것이 콤보 박스의 이벤트 리스너에 들어갈 것이라고 생각한다. – Josh

+0

@Josh 나는 무엇이든 할 수 없다. 이것은 마우스로 물건을 선택했기 때문에 발생합니다. Lista는 값을 확인하고 올바른 파일 (Browse From File)을 찾으면 OpenFileDialog를 열어야합니다. 이벤트 리스너는 어떻게 할 수 있습니까? –

+1

DataGrid 대신 DataGridView가 필요하거나 이상한 결과가 나올 수 있습니다. Lista의 for 루프에 중단 점을 설정하면 완료되면 곧바로 적용됩니다. 선택 사항이 datagridview 콤보 상자에서 변경 될 때가 아니라 CSV 파일을 선택하십시오. 각 콤보 박스에는 일반적으로 연결된 최대 SelectedValueChanged 및 SelectedIndexChanged 이벤트가 있습니다. 그러나 DataGridView에 있으면 CellValueChanged 이벤트를 처리하고 셀 값이 찾아보기 텍스트 인 경우 해당 위치에 OpenFileDialog를 넣어야 할 수 있습니다. – Josh

답변

0

DataGridView는 모든 행에 대해 하나의 편집 컨트롤 만 있습니다. 여기에 내가 비슷한 상황, 우선은 EditControlShowing 이벤트에 대리자를 시도를 처리하는 방법

네임 스페이스 WindowsFormsApplication1 { 공공 부분 Form1 클래스 : 양식 { 공개를 Form1() { 의 InitializeComponent(); }

OpenFileDialog ofd = new OpenFileDialog(); 
    private void Form1_Load(object sender, EventArgs e) 
    { 
    dataGridView1.Columns.Add("ID", "Product ID"); 

    DataGridViewComboBoxColumn comboxboxCol = new DataGridViewComboBoxColumn(); 
    comboxboxCol.HeaderText = "Type"; 
    comboxboxCol.Items.Add("Obj1"); 
    comboxboxCol.Items.Add("Obj2"); 
    comboxboxCol.Items.Add("Obj3"); 

    dataGridView1.Columns.Add(comboxboxCol); 

    dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(Grid_EditingControlShowing); 
    } 

    void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     ComboBox combo = e.Control as ComboBox; 
     if (combo != null) 
     { 
      // the event to handle combo changes 
      EventHandler comboDelegate = new EventHandler(
       (cbSender, args) => 
       { 
        ofd.ShowDialog(); 
       }); 

      // register the event with the editing control 
      combo.SelectedValueChanged += comboDelegate; 

     } 

    } 
} 

}

희망이

관련 문제