我有一个
BindingList<T>
Run Code Online (Sandbox Code Playgroud)
绑定到datagridview.我班上的一个属性需要很长时间来计算,所以我对这个动作进行了操作.在计算之后,我引发OnPropertyChanged()事件以通知网格值已准备好.
至少,这就是理论.但是,由于从一个差异线程调用了OnPropertyChanged方法,我在网格的OnRowPrePaint方法中得到了一些已经存在的异常.
任何人都可以告诉我如何在主线程中推出OnPropertyChanged事件吗?我不能使用Form.Invoke,因为类MyClass不知道它在Winforms应用程序中运行.
public class MyClass : INotifyPropertyChanged
{
public int FastMember {get;set;}
private int? slowMember;
public SlowMember
{
get
{
if (slowMember.HasValue)
return slowMember.Value;
else
{
Thread t = new Thread(getSlowMember);
t.Start();
return -1;
}
}
}
private void getSlowMember()
{
Thread.Sleep(1000);
slowMember = 5;
OnPropertyChanged("SlowMember");
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangingEventHandler eh = PropertyChanging;
if (eh != null)
{
eh(this, e);
}
}
}
Run Code Online (Sandbox Code Playgroud) 在Visual Basic项目中,我添加了一个包含一堆图像的资源文件(resx).
现在我想查询图像的名称.如果我在Visual Studio IDE的设计器视图中打开resx文件并选择一个图像,属性网格会显示一个名称属性(默认为"没有扩展名但可以更改的文件名").
背景是我有一个在运行时创建的图像列表,并填充了资源文件中的图像.为了能够通过密钥访问这些图像,我必须设置它.
我的代码看起来像这样(所有硬编码):
Dim imagelist as new Imagelist
imageList.Images.Add("A", My.Resources.MyImages.A)
imageList.Images.Add("B", My.Resources.MyImages.B)
imageList.Images.Add("C", My.Resources.MyImages.C)
imageList.Images.Add("D", My.Resources.MyImages.D)
imageList.Images.Add("E", My.Resources.MyImages.E)
....
imageList.Images.Add("XYZ", My.Resources.MyImages.XYZ)
Run Code Online (Sandbox Code Playgroud)
我希望得到这个:
Dim imagelist as new ImageList
For Each img in GetMeAllImagesWithNameFromMyResourceFile
imageList.Images.Add(img.Name, img.ImageFile)
Next
Run Code Online (Sandbox Code Playgroud)
其中Name是一个字符串,ImageFile是一个System.Drawing.Bitmap
在我的Windows窗体应用程序客户端有时报告一个奇怪的异常
System.InvalidOperationException: Value Dispose() cannot be called while doing CreateHandle()
at System.Windows.Forms.Control.Dispose(Boolean disposing)
at System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
at System.ComponentModel.Component.Dispose()
at MyCompany.SomeApp.DialogBox.Show(string caption, string message)
at MyCompany.SomeApp.MainForm.Button1_Click(Object sender, MouseEventArgs e)
Run Code Online (Sandbox Code Playgroud)
导致此错误的代码如下所示:
namespace MyCompany.SomeApp
{
public class DialogBox : CustomForm
{
public static DialogResult Show(string caption, string message)
{
using (DialogBox dialog = new DialogBox())
{
dialog.Text = caption;
dialog.lblMessage.Text = message;
return dialog.ShowDialog();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
DialogBox基本上是一个继承自Windows.Forms.Form的类,并做了一些设计更改,没什么特别的.例外情况发生在
return dialog.ShowDialog();
Run Code Online (Sandbox Code Playgroud)
而不是using像我期望的那样在街区尽头.看起来在某种程度上,在ShowDialog()方法中,在创建表单句柄之前,调用Dispose()方法.但我的DialogBox既没有调用Dispose()本身也没有吞下其他异常,它只在OnPaint()事件中做了一些绘画.
有没有人有一些线索如何摆脱这个例外?
更新:
这是我的CustomForm类中唯一的代码(除了Windows窗体设计器中的更改模式(添加了2个标签,一个按钮并更改了一些颜色)
Public Class CustomForm
Inherits …Run Code Online (Sandbox Code Playgroud) 我有一个 System.Windows.Forms DataGridView 绑定到List<MyObject>.
MyObject 类包含绑定到 DataGridView 中的 DataGridViewCheckboxCell 的布尔属性。
public class MyObject
{
public decimal DefaultValue {get; set; }
public bool HasCustomValue {get;set; }
public decimal CustomValue {get;set; }
public decimal CurrentValue
{
get
{
return HasCustomValue
? CustomValue
: DefaultValue;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我更改HasCustomValue另一个(只读)属性CurrentValue的值也会更改它的值。这是通过实现 INotifyPropertyChanged 事件来完成的(为了简单起见,我将该部分留在了源示例中)
如果我HasCustomValue从 DataGridView 外部更改,绑定到的列CurrentValue会立即更新。但是,如果用户启用/禁用复选框,HasCustomValue则不会在基础数据源中更改,除非他通过单击鼠标或按 TAB 键离开该列。
有没有办法在更改复选框值后强制网格直接更新数据源?
如果我绑定了一个控件属性,我可以设置为DataSourceUpdateMode,Windows.Forms.DataSourceUpdateMode.OnPropertyChanged但我在 DataGridView 中没有找到类似的东西
我有两个表orders和orderdetails
表顺序(PK = id,orderno上的UNIQUE索引)
|id|orderno|
| 1|1000 |
| 2|1001 |
Run Code Online (Sandbox Code Playgroud)
table orderdetails(PK = id)
|id|orderid|item|qty|
| 1| 1|ABC | 3|
| 2| 1|XYZ | 4|
Run Code Online (Sandbox Code Playgroud)
现在我想查询数据:
SELECT o.orderno, od.item, od.qty
FROM orders o
Run Code Online (Sandbox Code Playgroud)
INNER JOIN orderdetails od ON.orderno = od.order
返回:
|orderno|item|qty|
|1000 |ABC | 3|
|1000 |XYZ | 4|
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用以下代码将结果加载到DataTable中,它将失败:
var connectionString = "Server=localhost;Database=orders;Uid=root;";
var commandText = "SELECT o.orderno, od.item, od.qty" + Environment.NewLine +
"FROM orders o" + Environment.NewLine +
"INNER JOIN orderdetails od ON …Run Code Online (Sandbox Code Playgroud) 我偶然发现了我的代码中的一个方法,其中在我的代码中计算了舍入值.我知道比较双值产生意外结果的问题.
例
double x = 19.08;
double y = 2.01;
double result = 21.09;
if (x + y == result)
{
// this is never reached
}
Run Code Online (Sandbox Code Playgroud)
这里的解释:http://csharpindepth.com/Articles/General/FloatingPoint.aspx
但是,到目前为止,我预计Math.Round()方法即使使用double值也是准确的.
看看这段代码.
var decimals = 2;
var value1 = 4.725;
var value2 = 4.725M;
var result1 = Math.Round(value1, decimals, MidpointRounding.ToEven);
var result2 = Math.Round(value1, decimals, MidpointRounding.AwayFromZero);
var result3 = Math.Round(value2, decimals, MidpointRounding.ToEven);
var result4 = Math.Round(value2, decimals, MidpointRounding.AwayFromZero);
Console.WriteLine("Double (ToEven): {0}", result1); // outputs 4.72
Console.WriteLine("Double (AwayFromZero): {0}", result2); …Run Code Online (Sandbox Code Playgroud) 我想使用 AutoMapper 导入数据。
我的目标类都继承自一个基类Entity,该基类定义了一些在源 ( CreatedOn, CreatedBy, ModifiedOn, ModifiedBy)中不存在的属性
假设我有一个源类 Unit:
public class UnitOld
{
public int Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的目标课程单元:
public class Unit : Entity
{
public Guid UnitId { get; set; }
public string UnitName { get; set; }
}
public class Entity
{
public string CreatedOn { get; set; }
public string CreatedBy { get; set; }
public string ModifiedOn { get; set; } …Run Code Online (Sandbox Code Playgroud) 我有一个sql server紧凑框架数据库,我想查询一个int列
该列可以包含1到99999999之间的值,我对正确的4位数感兴趣
例子:
1 -> 1
12 -> 12
123 -> 123
1234 -> 1234
12345 -> 2345
123456 -> 3456
Run Code Online (Sandbox Code Playgroud)
我可以将结果转换为字符串并使用子字符串,但可以有一个更好的解决方案.
如果我从收件箱中选择Outlook邮件并将其复制到剪贴板,我可以将其作为*.msg文件粘贴到桌面.
现在我想为我的应用程序实现相同的功能.
Clipboard对象包含以下元素:
RenPrivateSourceFolder
RenPrivateMessages
RenPrivateItem
FileGroupDescriptor
FileGroupDescriptorW
FileDrop
FileNameW
FileName
FileContents
Object Descriptor
System.String
UnicodeText
Text
Run Code Online (Sandbox Code Playgroud)
FileGroupDescriptor包含MemoryStream带文件名的文件(Subject.msg),但我不知道如何从剪贴板数据中创建Outlook消息的副本,因为没有任何元素似乎包含消息本身.
有什么建议?
我有桌子items和桌子item_attributes.
为简单起见,假设我的表项有一列id和一列name.对于cource,id列上有一个索引.
该item_attributes表具有的列id,item_id,attribute_name和attribute_value和索引ONattrubute_name
现在我想查询具有特定属性的所有项目而不使用连接.
我使用以下查询执行此操作:
SELECT *
FROM items i
WHERE i.id IN (
SELECT item_id
FROM item_attributes a
WHERE a.attribute_name = 'SomeAttribute'
AND a.attribute_value = 'SomeValue'
)
Run Code Online (Sandbox Code Playgroud)
SubQuery本身运行得很快.
如果我首先执行查询本身并将结果用于IN查询
SELECT *
FROM items i
WHERE i.id IN (1,3,5,7,10,...)
Run Code Online (Sandbox Code Playgroud)
它也很快.
但是,组合查询非常非常慢(> 2秒).如果我调查查询计划,我明白为什么:MySQL对items表执行全表扫描,而不是先执行子查询并使用结果进行索引查询.
1, 'PRIMARY', 'items', 'ALL', '', '', '', '', 149726, 'Using where'
2, 'DEPENDENT SUBQUERY', 'item_attributes', 'index_subquery', 'IDX_ATTRIBUTE_NAME', …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×4
winforms ×3
datagridview ×2
mysql ×2
automapper ×1
clipboard ×1
datareader ×1
dispose ×1
double ×1
explain ×1
msg ×1
outlook ×1
resx ×1
sql ×1
sql-server ×1
vb.net ×1