如何获取WPF DataGrid以将更改保存回数据库?
我将DataGrid控件数据绑定到DataTable对象,并使用非常简单的SELECT查询填充该表,该查询检索一些基本信息.数据在控件中显示得很好.
但是当我使用控件编辑数据时,更改不会被推回到数据库.
有谁知道我错过了什么?
我必须维护一个具有许多文本数据类型的列的应用程序,其中插入多个值,用逗号,斜杠或有时甚至管道(|)字符分隔.我想弄明白你为什么要在地球上做这件事.
例如,订单表有一个名为详细信息的列,其中包含以下信息:
2x #ABC-12345 Widget, Black: $24.99 /4x #ABC-12344 Widget, Blue: $23.50
Run Code Online (Sandbox Code Playgroud)
其中/分离的行项目; 有一个VBScript代码,它从记录集中读取值并在一个For循环中解析它,以便使用类似的东西进行显示(这几乎就是代码读取的方式,变量名和所有内容)arydtls = split(rstmp("details"), "/").在整个代码中针对各种表重复该方法.
在我看来,它只是100倍更好(更不用说更容易使用)只是将细节放在一个单独的表中并链接回它(有趣的是,对于Orders它确实这样做,但数据不始终匹配详细信息文本字段,因为OrderDetail表在代码中更新;详细信息字段在应用程序中被视为只读.
我的前任是否知道我没有做过的事情,或者说"WTF?!!"我是对的.当我看这个架构?它看起来像是非常低效且难以维护,并且它使运行报告更加困难,因为我需要的数据可能包含在文本字段中,或者它可能位于十几个具有相似信息的表中并且用于不同的表中部分应用程序.
原始问题
我已经创建了一个函数,它等待特定字符串出现在串行端口上,并返回所有字符读取,直到找到该字符串,否则返回false.这很方便,但我想知道它是否被认为是不好的做法?
澄清:
主要目标是等待特定字符串在给定的时间内出现.除了IO错误,可能的结果是True(字符串确实出现)或False次要目标是获取整个输出,因为在寻找的实际答案之前可能存在我想要解析的信息.我想可能是我可以在一个返回值中合并主要和次要目标.
def MyFunc(s, timeout) :
test = get_some_input(timeout)
if test.endswith(s)
return test
else
return False
Run Code Online (Sandbox Code Playgroud)
编辑:另一个建议的答案是提出异常.我不认为这是一个好主意,因为超时是预期的行为.我的意思是,如果有一个用于指定超时的参数,那么超时是可能的结果,而不是例外.
编辑2:因为我需要存储输入,所以使用类是正确的解决方案.wait for函数具有明确的返回值,但是也可以访问在超时之前读取的整个字符串.
class Parser :
def __init__(self, sport_name):
self.currentMsg = ''
self.ser = serial.Serial(sport_name, 115200)
def WaitFor(self, s, timeOut=None):
self.ser.timeout = timeOut
self.currentMsg = ''
while self.currentMsg.endswith(s) != True :
# should add a try catch here
c=self.ser.read()
if c != '' :
self.currentMsg += c
else :
print 'timeout waiting for ' + s
return False
return True
Run Code Online (Sandbox Code Playgroud) 我创建了一个UserControl,每隔几秒就会使用串行端口的数据进行一次更新.此UserControl应该非常简单,包含字段名称的Label和包含字段值的另一个Label.我说它应该很简单,但它不起作用.它根本不更新,甚至不显示字段名称.
以下是代码:
public partial class LabeledField : UserControl {
public LabeledField() {
InitializeComponent();
}
public string fieldName {
get { return fieldNameLabel.Content.ToString(); }
set { fieldNameLabel.Content = value; }
}
public string fieldValue {
get { return (string)GetValue(fieldValueProperty); }
set { SetValue(fieldValueProperty, value); }
}
public static readonly DependencyProperty fieldValueProperty =
DependencyProperty.Register(
"fieldValue",
typeof(string),
typeof(LabeledField),
new FrameworkPropertyMetadata(
"No Data"
)
)
;
}
Run Code Online (Sandbox Code Playgroud)
这是XAML:
<UserControl x:Class="DAS1.LabeledField" Name="LF"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Orientation="Horizontal">
<Label Width="100" Height="30" Background="Gray" Name="fieldNameLabel" />
<Label …Run Code Online (Sandbox Code Playgroud) NP-completeness在我看来就像其中一个主要是理论上的东西,而不是你在正常工作环境中遇到的东西.
所以我有点好奇,如果有人在他们的工作中遇到问题,结果证明是NP完全的,并且设计需要改变以适应它吗?
我们有一个派生自DependencyObject的对象,并实现了一些DependencyProperties.
基本上是这样的:
class Context : DependencyObject {
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register ("Name", typeof (string), typeof (Context), new PropertyMetadata (""));
public string Name {
get {
return (string)this.GetValue (NameProperty);
}
set {
this.SetValue (NameProperty, value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,属性设置,可以绑定等.当我使用TwoWay绑定从WPF绑定到属性时问题出现了.TwoWay部分从未实际发生过,WPF从不调用此属性的集合.我已经设置了这样的绑定:
<TextBox Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Run Code Online (Sandbox Code Playgroud)
在这种情况下,键入文本框应立即更新Name属性,但不会.如果我将Name属性更改为常规POCO属性,它就可以工作(尽管TwoWay的另一面显然不会,除非我实现了INotifyPropertyChanged).
我在这做错了什么?这应该是一件非常简单的事情,但它让我头疼不已.
由于Delphi让你一直到方法的var部分来声明一个局部变量,你是否发现自己比在大学时更频繁地打破"Curly's Law"(重新使用变量)?(当然,除非你在大学编程Pascal).
如果是这样,你怎么做才能打破这种习惯,特别是在需要获取和/或设置大量属性的函数中.是否有一个门槛的地方是可以接受的申报TempInt : Integer和TempStr : String.(你有时使用'e' 而不是其他时间吗?)Temp
当我提供默认值时,为什么依赖项属性实现会使我的应用程序崩溃?
这段代码位于UserControl对象的类声明中.一切都很好 - 它编译和运行完美.
public static System.Windows.DependencyProperty DepProp
= System.Windows.DependencyProperty.Register( "Rect",
typeof(System.Windows.Shapes.Rectangle),
typeof(FooControl));
public System.Windows.Shapes.Rectangle Rect
{
get
{ return ((System.Windows.Shapes.Rectangle)(GetValue(DepProp))); }
set
{ SetValue(DepProp, value); }
}
Run Code Online (Sandbox Code Playgroud)
但是,当我将缺省值添加到依赖项属性时:
代码编译,但在尝试实例化UserControl时崩溃并发生致命异常.
作为参考,我的代码现在看起来像这样 - 添加了PropertyMetaData行:
public static System.Windows.DependencyProperty DepProp
= System.Windows.DependencyProperty.Register( "Rect",
typeof(System.Windows.Shapes.Rectangle),
typeof(FooControl),
new System.Windows.PropertyMetadata(new System.Windows.Shapes.Rectangle()));
public System.Windows.Shapes.Rectangle Rect
{
get
{ return ((System.Windows.Shapes.Rectangle)(GetValue(DepProp))); }
set
{ SetValue(DepProp, value); }
}
Run Code Online (Sandbox Code Playgroud)
从调用Register()中删除PropertyMetadata会导致程序完美运行,不会出现任何崩溃或任何其他问题.但我需要以后代码的默认值.如何在不崩溃的情况下让它接受默认值?
当它崩溃时,输出窗口中会显示以下异常:
A first chance exception of type 'System.ArgumentException' occurred in WindowsBase.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ImageButton控件进行客户端脚本执行.我可以使用OnClientClick属性指定要执行的客户端脚本,但是如何阻止它在每次用户单击时尝试发布?单击此按钮时没有理由发布.我已将CausesValidation设置为False,但这不会阻止它发布.
如果我想让一个线程暂停一段时间,直到满足某些条件,我可以双重锁定互斥锁吗?
这是我现在正在使用的想法的一个基本示例:
class foo
{
public:
static std::mutex processingMutex;
void infinite_processing_loop()
{
processingMutex.lock(); // Lock the mutex, initially
while(true)
{
if ( /*ready for this thread to process data*/ )
{
// ... perform one round of data processing ...
}
else // NOT ready for this thread to process data
{
/* Pause this thread,
and wait for another thread to unlock this mutex
(when more data might be ready for processing) */
processingMutex.lock();
}
}
processingMutex.unlock(); // Will …Run Code Online (Sandbox Code Playgroud) wpf ×4
.net-3.5 ×1
asp.net ×1
c++ ×1
data-binding ×1
datagrid ×1
delphi ×1
exception ×1
imagebutton ×1
mutex ×1
np-complete ×1
post ×1
postback ×1
python ×1
sql ×1
submit ×1
variables ×1
wpftoolkit ×1