我正在使用C#Visual Studio 2010开发用户控件 - 一种用于过滤datagridview的"快速查找"文本框.它应该适用于3种类型的datagridview数据源:DataTable,DataBinding和DataSet.我的问题是从DataSet对象过滤DataTable,它显示在DataGridView上.
可能有3个案例(标准WinForm应用程序的示例,其中包含DataGridView和TextBox) - 前2个工作正常,我遇到第3个问题:
1. datagridview.DataSource = dataTable:它可以工作,
所以我可以通过设置过滤:dataTable.DefaultView.RowFilter ="country LIKE'%s%'";
DataTable dt = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("country", typeof(string));
dt.Rows.Add(new object[] { 1, "Belgium" });
dt.Rows.Add(new object[] { 2, "France" });
dt.Rows.Add(new object[] { 3, "Germany" });
dt.Rows.Add(new object[] { 4, "Spain" });
dt.Rows.Add(new object[] { 5, "Switzerland" });
dt.Rows.Add(new object[] { 6, "United Kingdom" });
dataGridView1.DataSource = dt;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("DataSource …
Run Code Online (Sandbox Code Playgroud) 我想知道代码验证方法的参数在哪里 - 以及多久 - .
在下面的示例类(.dll库)中,您认为最好的方法是什么?假设我想验证,某些对象不能null
(但它可以是方法正常运行所需的任何其他验证).最好只在第1点检查一次,在用户可用的公共方法中,以及后来的"信任自己",在其他私有方法中,它不会为空,或者更好的有点偏执并且每个都检查一下时间它将被使用(在点2. 3.和4.)
如果我决定使用这些私有方法更改类中的某些内容,并且"忘记"传递有效对象,那么在使用该对象之前检查它(在第2,3,4点)将来会保护我.如果我将来添加一些新的公共方法,我也不必记住验证.另一方面 - 它一遍又一遍地检查相同的条件.或许你还有其他一些建议?
public class MyClass()
{
public MyClass()
{
}
public void ProcessObject(SomeObject obj)
{
//1. if (obj == null) throw new ArgumentException("You must provide valid object.");
DoSomething(obj);
DoMore(obj);
DoSomethingElse(obj);
}
private void DoSomething(SomeObject obj)
{
//2. if (obj == null) throw new ArgumenException("You must provide valid object.");
//do something with obj...
}
private void DoMore(SomeObject obj)
{
//3. if (obj == null) throw new ArgumentException("You must provide valid object."); …
Run Code Online (Sandbox Code Playgroud) 我一直在争论一个问题整整3天我找不到任何解决方案,请帮助:)
我使用Visual Studio 2010和C#语言.
我有一台像服务器一样工作的设备,它在非常不规则的时间段内发送一些数据(不可能定义任何读取超时).
我写了一个TCP客户端来连接到该服务器并读取数据.它工作正常,但是当网络出现问题并且服务器变得不可用时(例如,当我从计算机中拔出网线时),应用程序需要大约10秒才能"注意到"没有连接到服务器并且丢弃一个例外.(我不知道为什么到了10秒?它在哪里定义?我可以改变它吗?)
我想要更快地做出反应 - 让我们说连接断开后一秒钟.
然而谷歌搜索答案并没有为我提供任何有效的解决方案.
测试代码如下,我尝试在2个线程上进行:一个是读取数据,第二个是查找连接状态,当它被破坏时应该报警.它既不适用于TCPClient也不适用于Socket类.我曾尝试用tcpClient.SendTimeout = 100;
和读取/写入一些数据,stream.WriteTimeout = 100;
但它似乎不起作用.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace TCPClient
{
class Program
{
static volatile bool _continue = true;
static TcpClient tcpClient;
static NetworkStream stream;
static void Main(string[] args)
{
try
{
//client thread - listen from server
Thread tcpListenThread = new Thread(TcpListenThread);
tcpListenThread.Start();
//connection checking thread
Thread keepAliveThread = new Thread(KeepAliveThread);
keepAliveThread.Start();
while …
Run Code Online (Sandbox Code Playgroud) 在VS 2010 Express版(对于C#)中是否有可立即使用的代码片段,是否可以使用所选属性中的参数创建构造函数?
当我创建一个新类时,我编写了以下代码:
public class FileDetails
{
public int ID { get; set; }
public string FileName { get; set; }
public string FilePath { get; set; }
public DateTime LastWriteTime { get; set; }
public FileStatus LastFileStatus { get; set; }
public NotifyIfFileNotExists NotifyIfFileNotExists { get; set; }
public string RecepientsEmailList { get; set; }
public string AdminEmailList { get; set; }
public FileDetails()
{
}
}
Run Code Online (Sandbox Code Playgroud)
我想鼠标选择所有公共属性(或放置一些片段代码),为我生成以下costructor:
public FileDetails(int id, string fileName, string filePath, DateTime lastWriteTime, FileStatus …
Run Code Online (Sandbox Code Playgroud) 试图实现3层(不是:层,我只是想在逻辑上将我的项目分开,在一台机器上)架构我发现了很多不同的方法,我很困惑,最好的方法是什么(如果有的话)在WinForms应用程序中.
现在我毫不怀疑项目中应该存在的3个层次:
在UI中我放了所有的WinForms.必须还有一些逻辑用控件中的数据填充对象并将其传递给BLL层.
在DAL中,我想使用ADO.NET为数据操作添加类和方法,如:
public class OrderDAL
{
public OrderDAL()
{
}
public int Add(Order order)
{
//...add order to database
}
public int Update(Order order)
{
//...update order in database
}
//...etc.
}
Run Code Online (Sandbox Code Playgroud)
问题在于BLL和问题 - 我应该使用数据传输对象在层之间传递数据,还是应该通过整个类?
如果我选择使用DTO,那么我将创建额外的公共类Order
,即对UI,BLL和DAL的引用:
public class Order
{
public int Id { get; set; }
public DateTime Date { get; set; }
public string Number { get; set; }
public string CustomerName { …
Run Code Online (Sandbox Code Playgroud) 我(我想)我的Visual Studio 2010 Express环境有问题:当我设计自己的UserControl时,在属性网格中我看不到该控件的公共属性.但是,它们在项目中可见,引用此控件.
因为它是Express Edition,我创建了新的空项目,然后添加新的UserControl.
然后,为了测试,我输入以下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Project1
{
public partial class UserControl1 : UserControl
{
private int myNumber;
[Browsable(true)]
public int MyNumber
{
get
{
return myNumber;
}
set
{
myNumber = value;
}
}
public UserControl1()
{
InitializeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在VS 2008中,我记得,即使没有[Browsable(true)]
属性,也应该在属性网格中显示MyNumber 属性.但是,在VS 2010中,当我在解决方案资源管理器中双击UserControl1.cs并查看属性时,我看不到MyNumber.
当我在另一个项目中引用和使用此控件时,可以访问它的属性.
我试图竞争重新安装VS 2010环境,包括SP1,但没有成功.你知道什么是错的吗?
顺便说一下:这些属性都不起作用:
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Bindable(true)]
Run Code Online (Sandbox Code Playgroud)
最好的问候,
Marcin
(求助)我正在构建一个应用程序,它可以根据XML文件中的一些描述动态地创建它的一些控件.
我现在需要的是与TryParse()方法非常相似的东西:如果字符串变量中的文本可以转换(或解析)为一个类型,可以检查(没有抛出/捕获异常),我在其他变量中有这个名称(myType
).
问题是myType
可以是任何.NET类型:DateTime, Bool, Double, Int32
等等.
例:
string testStringOk = "123";
string testStringWrong = "hello";
string myType = "System.Int32";
bool test1 = CanCovertTo(testStringOk, myType); //true
bool test2 = CanCovertTo(testStringWrong, myType); //false
Run Code Online (Sandbox Code Playgroud)
CanCovertTo(string testString, string testType)
功能应该如何?
我得到的最接近的是以下代码:
private bool CanCovertTo(string testString, string testType)
{
Type type = Type.GetType(testType, null, null);
TypeConverter converter = TypeDescriptor.GetConverter(type);
converter.ConvertFrom(testString); //throws exception when wrong type
return true;
}
Run Code Online (Sandbox Code Playgroud)
但是,它在尝试从错误的字符串转换时抛出异常,我不想使用try {} catch()
它.
解:
private bool CanCovertTo(string testString, string …
Run Code Online (Sandbox Code Playgroud) 我想在我的WinForms应用程序中实现N层架构,以便从数据访问中分离(仅逻辑上 - 在一个项目中)业务逻辑,但是我对在BLL中使用transacion有一些疑问.我在互联网上找到的所有教程都是该架构的非常简单的实现(没有事务),或者对我的需求来说过于复杂.试图找到自己的方式,我已经到了这一点,我不知道处理BLL层事务的最佳方法.
我将尝试使用一些简单的示例来说明问题(所有类都在单独的文件中):
//DTO - Data Transfer Objects
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
public class SomeOtherItem
{
public int Id { get; set; }
public string Name { get; set; }
}
//DAL - Data Access layer
public class ItemDAL
{
public ItemDAL()
{
}
public void Add(Item item)
{
using (NpgsqlConnection conn = new NpgsqlConnection(connString))
{
conn.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand())
{ …
Run Code Online (Sandbox Code Playgroud) 虽然我使用 VBA for Excel 已经很长时间了,但我有一个问题无法自己解决。我已经在下面进行了描述,希望得到一些帮助或建议。
我使用的是 Excel 2007 和 Windows XP,它们都更新了最新的补丁。
我经常使用以下代码从另一个工作簿中获取数据:
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=g:\source.xls;Extended Properties=Excel 8.0;"
Sql = "SELECT Field1, Field2 FROM [Sheet1$]"
Set rst = New ADODB.Recordset
rst.Open Sql, conn, adOpenForwardOnly
Worksheets("Results").Range("A2").CopyFromRecordset rst
rst.Close
Set rst = Nothing
conn.Close
Set conn = Nothing
Run Code Online (Sandbox Code Playgroud)
尽可能简单 - 只需连接到文件并从中获取一些数据。只要位于公共网络驱动器 (g:\source.xls) 上的源文件没有在另一台计算机上打开,它就可以完美运行。
当另一台计算机上的某个用户打开该文件并尝试执行以下代码时,我注意到一件事我想摆脱:源 Excel 文件在我的计算机上打开(以只读模式)并且在与该文件的连接关闭后它不会关闭。更糟糕的是,即使我手动关闭了这个源文件,它也会在我的文件中留下一些垃圾,就像它从未关闭一样:在执行几次代码后查看图片(源文件之前已关闭):
我开始相信这是一个无法解决的错误 - 希望我错了 :)
我有以下示例字符串:
$0$aaaaa$1$bbbbb$2$cccccc
美元标记之间有一些数字.我想用Regex用数字+ 1替换每个数字,所以输出应该是:
$1$aaaaa$2$bbbbb$3$cccccc
如何在C#中使用Regex?
我知道如何找到所有数字:
string s = "$0$aaaaa$1$bbbbb$2$cccccc";
Regex regex = new Regex(@"\$(?<myNumber>.*?)\$");
MatchCollection matches = regex.Matches(s);
foreach (Match match in matches)
{
Console.WriteLine(match.Groups["myNumber"]);
}
Console.WriteLine("\n\nFinal string = " + s);
Console.ReadKey();
Run Code Online (Sandbox Code Playgroud)
通过regex.Replace(...)
我可以用一个值来替换他们,但我不知道如何使用正则表达式逐个更换号码.
有谁知道这是怎么做到的吗?
最好的问候,
Marcin
我想在C#Visual Studio 2010中创建标准控件的用户控件,它在设计时保持独立.
问题是,当我创建这样的标准用户控件时:
public partial class MyUserControl: UserControl
{
public MyUserControl()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)
我在其上添加了2个按钮和1个文本框,它们成为UserControl的一部分.
然后,当设计控件并将其添加到表单上时,我可以移动它,调整大小等,但我无法单击或选择Button1或Button2或TextBox1.
另外,双击button1不会创建button1_click事件,而是创建UserControl_Load事件.
我希望实现类似于DataBindingNavigator控件的效果 - 您可以在其中单独单击每个按钮.
我知道我可以创建公共属性设置/返回用户控件按钮或文本框,但它们将出现在设计器中,将无法选择它们.
您认为可以使用标准控件吗?如果是这样,怎么办?
最好的问候,
mj82