구버,
은 내가 당신의 질문에 대답 할 것입니다 확실하지 않다,하지만 난 방법을 코드에서 DataGrid
에 정보를 결합하는 방법을 보여주고, 바인딩 양방향으로 정보를 설정합니다.
먼저 단방향, 일회성 및 양방향 바인딩의 차이점에 대해 설명하는 link입니다.
는 편집 데이터 그리드에서 일어났다 후 데이터 소스를 변경할 수있는 세 가지 방법이 있습니다 :
먼저 수동으로 XAML의 열을 설정할 수 있습니다 는 각 변수에 바인딩을 나타냅니다 '양방향'.
<data:DataGrid Name="data" AutoGenerateColumns="False">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Test"
Binding="{Binding test, Mode=Two-Way}"/>
</data:DataGrid.Columns>
</data:DataGrid>
당신은 코드 숨김 페이지에서 같은 일을 할 수 두 번째.
rpdata.ItemsSource = info;
rpdata.Columns.Clear();
DataGridTextColumn user = new DataGridTextColumn();
user.Header = "User";
user.Binding = new System.Windows.Data.Binding("User");
user.Binding.Mode = BindingMode.TwoWay;
rpdata.Columns.Add(user);
rpdata
은 데이터 격자를 나타냅니다. 아래 그림과 같이
셋째
대신 수동으로 각 열을 연결하는, 당신은 데이터 그리드에 'CellEditEnded'이벤트를 추가 할 수 있습니다.
및 이벤트에 대한 코드의
, 다음과 같은 코드를 추가 할 수 있습니다 :
xt.CommitEdit();
이 데이터 소스를 업데이트합니다.
데이터 그리드에 의해 업데이트 된 엔티티를 사용하는 서비스에 대한 새 계약서를 만들어야합니다.
[OperationContract]
public void UpdateWork(List<Assumptions> updates)
여기서 내 엔티티는 '가정'입니다.
해당 운영 계약서에 다음 코드를 추가하십시오. 많은 인터넷 검색 후
//Create a new entity datacontext
Entities ds = new Entities();
//For each of the entities in the list
foreach (Assumptions update in updates)
{
try
{
//In the datacontext find the entity with the same primary key as the updated one
var test = from x in ds.Assumptions
where x.ID.Equals(update.ID)
select x;
//Update the entity
test.First() = update;
}
catch (Exception e)
{
//If the entity does not exist, add it to the context
ds.AddToAssumptions(update);
}
}
//Save the context
ds.SaveChanges();
그래서 현재 내가 내 데이터베이스와 실버 데이터 그리드의 중간 남자로 ADO.Net 엔티티 프레임 워크를 사용하고 있습니다. 이 설정을 변경하여 데이터를 검색하고 표시하는 것부터 변경하고 데이터베이스에 다시 유지할 수 있도록하기 위해 수행해야 할 단계는 무엇입니까? (현재 실제로는 거의 일어나지 않기 때문에 현재 위의 코드가 내 프로젝트에 들어갈 수있는 곳을 볼 수 없습니다.) – Goober
DataGrid에 포함시킬 모든 편집 가능 열에 대해 새로운 ""을 정의해야합니까? – Goober
네, 아마도 데이터 소스를 그리드에 바인딩하고 rpdata.Columns를 반복하고 바인딩 모드를 양방향 (코드에서)으로 설정할 수 있습니다. 나는 아직 이것을 시험하지 않았다. – Johannes