这里是代码,我希望tabcontrol允许显示datagrid中的更改/但它不起作用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightClient.ServiceReference1;
namespace SilverlightClient
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
//onsubmit button clicked
//insert operation
DemoServiceClient webService = new DemoServiceClient();
webService.InsertDataCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(webService_InsertDataCompleted);
webService.InsertDataAsync(TestItem1TxtBox.Text, TestItem2TxtBox.Text);
}
void webService_InsertDataCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
//throw new NotImplementedException();
return;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//onget button clicked
//select operation
DemoServiceClient webService = new DemoServiceClient();
webService.GetRowsCompleted += new EventHandler<GetRowsCompletedEventArgs>(webService_GetRowsCompleted);
//results returned by this cannot be used directly
//as the method works asynchronously thereby allowing
//to proceed before the results are generated.
//there must do anything regarding results
//using the completed event handler
webService.GetRowsAsync();
}
void webService_GetRowsCompleted(object sender, GetRowsCompletedEventArgs e)
{
// ResultsGrid.ItemsSource = e.Result;
ResultsGrid.ItemsSource = null;
List<Table1> gamma= e.Result.ToList();
ResultsGrid.ItemsSource = gamma;
tabControl1.UpdateLayout();
// tabControl1.TabIndex = 1;
}
private void OnDeleteClick(object sender, RoutedEventArgs e)
{
//remove operation
Table1 selectedRow = ResultsGrid.SelectedItem as Table1;
// Now access the service to delete the item
DemoServiceClient webService = new DemoServiceClient();
webService.DeleteRowAsync(selectedRow.KEY);
// Now refresh the grid
webService.GetRowsCompleted += new EventHandler<GetRowsCompletedEventArgs>(webService_GetRowsCompleted);
webService.GetRowsAsync();
}
private void button4_Click(object sender, RoutedEventArgs e)
{
//UPDATE BUTTON
//get the selected row
Table1 selectedRow = ResultsGrid.SelectedItem as Table1;
//declare the web service
DemoServiceClient webService = new DemoServiceClient();
//call the update method
//do pass the GUID and the UPDATE text
webService.UpdateDataAsync(selectedRow.KEY, textBox1.Text);
//assign the event handler
webService.GetRowsCompleted += new EventHandler<GetRowsCompletedEventArgs>(webService_GetRowsCompleted);
//call get rows
webService.GetRowsAsync();
}
}
}
<UserControl x:Class="SilverlightClient.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="600" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White" Height="600" Width="600">
<Grid.RowDefinitions>
<RowDefinition Height="20*" />
<RowDefinition Height="555*" />
<RowDefinition Height="25*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="66*" />
<ColumnDefinition Width="468*" />
<ColumnDefinition Width="66*" />
</Grid.ColumnDefinitions>
<sdk:TabControl Grid.Column="1" Grid.Row="1" Height="555" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="468" BorderThickness="0,2,0,0">
<sdk:TabItem Header="Insert Data" Name="tabItem1" FontFamily="Trebuchet MS">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="328*" />
<ColumnDefinition Width="128*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="65*" />
<RowDefinition Height="74*" />
<RowDefinition Height="22*" />
<RowDefinition Height="122*" />
<RowDefinition Height="235*" />
</Grid.RowDefinitions>
<sdk:Label Height="64" HorizontalAlignment="Left" Margin="2,1,0,0" Name="label1" VerticalAlignment="Top" Width="106" Content="TestItem1" Grid.Column="1" />
<TextBox Height="52" Margin="0,1,4,0" x:Name="TestItem1TxtBox" VerticalAlignment="Top"/>
<sdk:Label Height="64" HorizontalAlignment="Left" Margin="2,0,0,0" Name="label2" VerticalAlignment="Top" Width="106" Content="TestItem2" Grid.Row="1" Grid.Column="1" />
<TextBox Height="64" x:Name="TestItem2TxtBox" VerticalAlignment="Top" Margin="0,0,4,0" Grid.Row="1" />
<Button Content="Submit" Height="23" Name="button2" Width="133" Click="button2_Click" Margin="191,0,4,121" Grid.Row="2" Grid.RowSpan="2" />
<TextBox Grid.Row="3" Height="121" HorizontalAlignment="Left" Name="textBox1" VerticalAlignment="Top" Width="325" />
<TextBlock Grid.Column="1" Grid.Row="3" Height="121" HorizontalAlignment="Left" Name="textBlock1" Text="Update Text for TEXT item 2, please select guig key by using the VIEW/DELETE DATA TAB" VerticalAlignment="Top" Width="123" TextWrapping="Wrap" />
</Grid>
</sdk:TabItem>
<sdk:TabItem x:Name="TABview" Header="View/Delete Data">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="29*" />
<RowDefinition Height="155*" />
<RowDefinition Height="24*" />
<RowDefinition Height="27*" />
<RowDefinition Height="286*" />
</Grid.RowDefinitions>
<sdk:Label HorizontalAlignment="Left" Name="label3" VerticalAlignment="Stretch" Width="173" Content="Datagrid Silverlight" Margin="0,3,0,0" />
<sdk:DataGrid AutoGenerateColumns="True" Height="155" HorizontalAlignment="Left" Name="ResultsGrid" VerticalAlignment="Top" Width="456" Cursor="Hand" GridLinesVisibility="All" Grid.Row="1" />
<Button Content="Get" Height="23" HorizontalAlignment="Left" Margin="135,2,0,0" Name="button1" VerticalAlignment="Top" Width="149" Click="button1_Click" />
<Button Content="Delete" Height="26" HorizontalAlignment="Left" Margin="381,0,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="OnDeleteClick" />
<Button Content="Update" Grid.Row="3" Height="27" HorizontalAlignment="Left" Margin="320,0,0,0" Name="button4" VerticalAlignment="Top" Width="132" Click="button4_Click" />
</Grid>
</sdk:TabItem>
</sdk:TabControl>
</Grid>
</UserControl>
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
namespace WcfSqlDemoWeb
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DemoService
{
// Return the list of valid data
[OperationContract]
public List<Table1> GetRows()
{
DataClasses1DataContext db = new DataClasses1DataContext();
var selected_rows = from rows in db.Table1s select rows;
return selected_rows.ToList();
}
// Insert a new data into the database
[OperationContract]
public void InsertData(string testItem1,
string testItem2)
{
DataClasses1DataContext db = new DataClasses1DataContext();
// Create a new row object.
Table1 row = new Table1
{
KEY = Guid.NewGuid(),
TestItem1 = testItem1,
TestItem2 = testItem2
};
// Add the new object to the collection.
db.Table1s.InsertOnSubmit(row);
// Submit the change to the database.
db.SubmitChanges();
db.Dispose();
return;
}
[OperationContract]
public void UpdateData(Guid key, string update_text)
{
DataClasses1DataContext db = new DataClasses1DataContext();
// get the row with the key as a single tuple
var original_row = (from rows in db.Table1s where rows.KEY == key select rows).Single();
// get the row with the key
var delete_row = from rows in db.Table1s where rows.KEY == key select rows;
//a new instance of the table
//assiging it values as needed
Table1 changed_table = new Table1
{
KEY = key,
TestItem1 = original_row.TestItem1,
TestItem2 = update_text
};
//delete the row completely with the original selected record
db.Table1s.DeleteAllOnSubmit(delete_row);
//now insert the newly created TABLE INSTANCE
db.Table1s.InsertOnSubmit(changed_table);
// Submit the change to the database.
db.SubmitChanges();
}//update data ends
// Delete the item specified by the passed key
[OperationContract]
public void DeleteRow(Guid key)
{
DataClasses1DataContext db = new DataClasses1DataContext();
var selected_row = from rows in db.Table1s where rows.KEY == key select rows;
// Delete the selected "rows". There will actual be only one
// item in this collection because the Guid is unique and is the
// primary key for the table.
db.Table1s.DeleteAllOnSubmit(selected_row);
// Submit the change to the database.
db.SubmitChanges();
}
/*
//here it will give a error message stating,
//the sql return type is not serializable
//please make a datacontract and then try.
//so simply put, support for this type of data handling is being
//removed
// sql select all
[OperationContract]
public SqlDataReader sqlSELECT()
{
string connectionstring = @"Data Source=DEVELOPMENT-PC\SQLEXPRESS;Initial Catalog=SILVERLIGHT;Integrated Security=True;Pooling=False";
SqlConnection connection = new SqlConnection();
connection.ConnectionString = connectionstring;
SqlCommand command = new SqlCommand();
command.CommandText = "select * from Table1";
command.CommandType = CommandType.Text;
command.Connection = connection;
connection.Open();
var results_by_sql = command.ExecuteReader();
return results_by_sql;
}
*/
// Add more operations here and mark them with [OperationContract]
}
}
答案 0 :(得分:2)
试试这个...它不是你特定问题的答案,而只是一种快速的方式让它以一种可以被认为是“更好”的方式运作。它使用带有通知属性的绑定来为网格提供数据。我只包含了您需要更改的相关代码,您可以填写其余部分。
namespace SilverlightClient
{
public partial class MainPage : UserControl, INotifyPropertyChanged
{
public MainPage()
{
InitializeComponent();
this.dataContext = this;
}
void webService_GetRowsCompleted(object sender, GetRowsCompletedEventArgs e)
{
MyResults = e.Result.ToList();
tabControl1.UpdateLayout();
}
protected List<Table1> MyResults
{
get { return _myResults; }
set
{
if (_myResults != value)
{
_myResults = value;
OnPropertyChanged("MyResults");
}
}
}
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArg(propertyName));
}
private List<Table1> _myResults = null;
public event PropertyChangedEventHandler PropertyChanged;
}
}
<sdk:DataGrid Name="ResultsGrid"
... etc ...
DataContext="MyResults"
/>