向WPF中的行和单元格添加值

向WPF中的行和单元格添加值,wpf,Wpf,我正在用datagridview开发一个程序。 我需要一些关于向行和单元格添加值的帮助: 我的代码: for (int i = 0; i < 0x18; i++) { this.dataview1.UpdateDefaultStyle(); this.dataview1.Rows[i].Cells[0].Value = i; this._dataview1[i].Cel

我正在用datagridview开发一个程序。 我需要一些关于向行和单元格添加值的帮助:

我的代码:

  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();
            }
for(int i=0;i<0x18;i++)
{
this.dataview1.UpdateDefaultStyle();
this.dataview1.Rows[i]。单元格[0]。值=i;
此.u dataview1[i].Cells[1].Value=GetName(i);
Application.DoEvents();
}
它不工作,我得到这个错误:System.Windows.Controls.DataGrid不包含行。 单元格也有同样的错误。它在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>

请帮帮我!
我希望我说得足够清楚。

试试这个。WPF具有神奇的绑定

您只需将项添加到集合(您的源)WPF绑定,DataGrid将为您添加行

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

</DataGrid.Columns>
</DataGrid>

ViewModel/CodeBehind

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
公共类MyClient
{
公共字符串名称{get;set;}
公共字符串客户端{get;set;}
}
ObservableCollection客户=新ObservableCollection();
//将数据添加到客户集合,因为它是可观察的集合,当您在OB中添加/删除项目时,它会通知客户集合。收集
**你再加一份**

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

**Collection Updated the datagrid also will be updated**
for(int i=0;i<0x18;i++)
{
添加(newmyclients(){Client=“value”,Name=“value”});
}
**集合已更新数据网格也将更新**
参见参考文献


试试这个。WPF具有神奇的绑定

您只需将项添加到集合(您的源)WPF绑定,DataGrid将为您添加行

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

</DataGrid.Columns>
</DataGrid>

ViewModel/CodeBehind

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
公共类MyClient
{
公共字符串名称{get;set;}
公共字符串客户端{get;set;}
}
ObservableCollection客户=新ObservableCollection();
//将数据添加到客户集合,因为它是可观察的集合,当您在OB中添加/删除项目时,它会通知客户集合。收集
**你再加一份**

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

**Collection Updated the datagrid also will be updated**
for(int i=0;i<0x18;i++)
{
添加(newmyclients(){Client=“value”,Name=“value”});
}
**集合已更新数据网格也将更新**
参见参考文献


更新的代码在这里

<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>
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;

ViewModel代码在这里

<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>
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;
公共类员工
{
公共字符串名称{get;set;}
公共字符串公司{get;set;}
}
ObservableCollection客户=新ObservableCollection();
对于(int i=0;i<0x18;i++)
{
Emp.Add(newemployee(){Name=“Kalsa”,Company=“Google”});
}
dg.ItemsSource=Emp;

更新的代码在这里

<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>
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;

ViewModel代码在这里

<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>
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;
公共类员工
{
公共字符串名称{get;set;}
公共字符串公司{get;set;}
}
ObservableCollection客户=新ObservableCollection();
对于(int i=0;i<0x18;i++)
{
Emp.Add(newemployee(){Name=“Kalsa”,Company=“Google”});
}
dg.ItemsSource=Emp;

我看不到任何向“dataview1”添加行的代码。您确定在调用代码之前控件中有行吗?@AndrewMack我已经尝试过:尝试{this.dataview1.IsEnabled=true;this.dataview1.RowCount=0x18;}catch{MessageBox.Show(“连接失败”);}@Christos92,检查教程first@dzkozl谢谢,但这不是我要找的!在WPF控件上使用WinForms方法会使它变得更加复杂,如果不是的话,在某些情况下是不可能的。当您使用WPF时,您使用绑定。我没有看到任何将行添加到“dataview1”的代码。您确定在调用代码之前控件中有行吗?@AndrewMack我已经尝试过:尝试{this.dataview1.IsEnabled=true;this.dataview1.RowCount=0x18;}catch{MessageBox.Show(“连接失败”);}@Christos92,检查教程first@dzkozl谢谢,但这不是我要找的!在WPF控件上使用WinForms方法会使它变得更加复杂,如果不是的话,在某些情况下是不可能的。当您使用WPF时,您可以使用绑定。谢谢它可以工作。bud:)我很少编辑代码,因为我在名称处有一个值:public string getname1(int client){byte[]buffer=new byte[20];PS3.GetMemory(0x178646c,buffer);string names=Encoding.ASCII.GetString(buffer);names=names.Replace(“\0”,”);返回Name;}@Christos92快乐编码。感谢它能工作。bud:)我编辑了一点代码,因为我在Name处有一个值:public string getname1(int client){byte[]buffer=new byte[20];PS3.GetMemory(0x178646c,buffer);string names=Encoding.ASCII.GetString(buffer);names=names.Replace(“\0”,”);返回names;}@Christos92。