2014-05-16 2 views
1

나는 datagridview를 사용하여 프로그램을 개발하고있었습니다. 내가 행과 셀에 값을 추가하는 몇 가지 도움이 필요합니다WPF의 행과 셀에 값 추가

내 코드 : System.Windows.Controls.DataGrid은 행을 포함하지 않는 :

for (int i = 0; i < 0x18; i++) 
      { 
       this.dataview1.UpdateDefaultStyle(); 
       this.dataview1.Rows[i].Cells[0].Value = i; 
       this._dataview1[i].Cells[1].Value = GetName(i); 
       Application.DoEvents(); 
      } 

그것은 내가 오류로 얻을 일을 해달라고합니다. 셀에 같은 오류가 있습니다. 그것은 C#에서 잘 작동합니다. 내 wpf 코드;

<Grid Background="#FFE5E5E5"> 
    <DataGrid x:Name="dataview1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="337" Width="809"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Client"/> 
      <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Name"/> 
     </DataGrid.Columns> 
    </DataGrid> 

제발 도와주세요! 나는이 사실을 충분히 분명히하기를 희망한다.

+0

...

<DataGrid x:Key="dg" ItemsSource="{Binding}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{Binding Name}" /> <DataGridTextColumn Header="Company" Binding="{Binding Company}" /> </DataGrid.Columns> </DataGrid> 

뷰 모델 코드가 여기에 ... 여기에 'dataview1'에 행을 추가하는 모든 코드를 참조하십시오. 코드가 호출되기 전에 컨트롤에 행이 있는지 확 인해 야합니까? –

+0

@AndrewMack 이미 시도 : 시도 { this.dataview1.IsEnabled = true; 0x35; } catch { MessageBox.Show ("연결 실패."); } – Christos92

+0

@ Christos92, [this] (http://wpftutorial.net/DataGrid.html) 자습서를 먼저 확인하십시오. – dkozl

답변

4

시도해보십시오. WPF는 매직 바인딩을 가지고

당신은 당신

<DataGrid ItemsSource="{Binding Customers}" AutoGenerateColumns="False" > 
<DataGrid.Columns> 
     <DataGridTextColumn Header="Client" Binding="{Binding Client}" /> 
     <DataGridTextColumn Header="Name" Binding="{Binding Name}" /> 

</DataGrid.Columns> 
</DataGrid> 

뷰 모델/숨김

public class MyClients 
{ 

public string Name { get; set; }  
public string Client{ get; set; } 

} 

ObservableCollection<MyClients> Customers=new ObservableCollection<MyClients>(); 

// Add data to the customer collection , as it is OBSERVABLE collection its notify when you add/remove item in OB. Collection 

에 대한 행을 추가합니다 WPF 바인딩 및 데이터 그리드 컬렉션 (소스)에 항목을 추가 할 필요가 ** 부분 추가 ​​**

 for (int i = 0; i < 0x18; i++) 
     { 
      Customers.Add(new MyClients(){Client="value",Name="value"}); 
     } 

**Collection Updated the datagrid also will be updated** 

페이지 참조 Link1

link2

link 3

+0

감사는 꽃 봉오리를 작동 :) 내가 이름에 값이 있었기 때문에 내가 약간의 코드를 편집 : 공공 문자열 getname1을 (INT 클라이언트) { 바이트 [] 버퍼 = 새로운 바이트 [20] PS3.GetMemory (0x178646c, buffer); 문자열 이름 = Encoding.ASCII.GetString (버퍼); names = names.Replace ("\ 0", ""); 이름을 반환합니다. } – Christos92

+0

@ Christos92 해피 코딩. – Eldho

0

업데이트 된 코드는 그렇게하지

public class Employee 


{ 

public string Name { get; set; }  
public string Company{ get; set; } 

} 
ObservableCollection<Employee> Customers=new ObservableCollection<Employee>(); 
for (int i = 0; i < 0x18; i++) 
     { 
      Emp.Add(new Employee(){Name="Kalsa",Company="Google"}); 
     } 
     dg.ItemsSource=Emp;