-
C#读取文件内容显示在DataGridView表格中
Visual C#在DataGridView单元格中显示文本文件的内容,可以理解为,从外部读取文件内容,将其显示在DataGridView
单元格中,可以看出,本代码中使用了DataTable对象、OleDb.OleDbDataAdapter对象,外部文件的格式暂定为txt,其它格式需要相关解析组件支持,实现本功能并不难,以下几行代码就可大致实现:
string MyPath = System.IO.Directory.GetCurrentDirectory();
string MyConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+MyPath+";Extended Properties="text;HDR=yes;FMT=delimited";";
string MySQL = "select * from 季度订单.txt";
DataTable MyTable = new DataTable();
System.Data.OleDb.OleDbDataAdapter MyAdapter = new System.Data.OleDb.OleDbDataAdapter(MySQL, MyConnectionString);
MyAdapter.Fill(MyTable);
this.dataGridView1.DataSource = MyTable;
运行界面效果请参见下图,完整源码请在本页下载。
- 2022-03-20 07:42:37下载
- 积分:1
-
Visual C# LINQ转换为一对多字典
Visual C# 转换为一对多字典,构造泛型列表,使用LINQ查找用户代码小于3的列表,此时的query变量的类型是IEnumerable类型,使用ToLookup方法将query转换为一对多字典类型:
ILookup userLookup = query.ToLookup(itm => itm.UserCode);
label1.Text = "ILookup的结果是:
";
foreach (var user in userLookup)//遍历查询结果
{
label1.Text += user.Key;//显示主键
label1.Text += " ";
foreach (var user2 in user)//遍历所有值
{
label1.Text += user2.UserName + " , ";//显示键值
}
label1.Text += "
";
}
- 2023-05-18 03:20:03下载
- 积分:1
-
STM32飞控baseflight-master,主流飞控
STM32飞控baseflight-master,主流飞控,更新快,性能好。
- 2023-02-11 09:40:04下载
- 积分:1
-
C# 使用字符串数组创建一组单选框数据
C# 基于字符串数组创建一组单选按钮的例子,附上了例子源代码,大家可了解C#字符串数组的简单应用。
以下是例子中的代码,运行后可见如下图所示的图片效果:
//基于字符串数组创建一组单选按钮
string[] MyArray = new string[4];
MyArray[0] = "渝北区";
MyArray[1] = "巴南区";
MyArray[2] = "长寿区";
MyArray[3] = "南岸区";
RadioButton[] MyRadioButtons =new RadioButton[4];
for (int i = 0; i < 4; ++i)
{
MyRadioButtons[i] = new RadioButton();
MyRadioButtons[i].Text = MyArray[i];
MyRadioButtons[i].Location = new System.Drawing.Point(
10, 20 + i * 20);
this.groupBox1.Controls.Add(MyRadioButtons[i]);
}
this.groupBox1.Text = "请评选全市卫生城区:";
- 2022-02-27 00:14:46下载
- 积分:1
-
C# 使用定时器动画摆动显示图形
C# 使用定时器动画摆动显示图形,利用定时器可改变物体运动的振幅、角频率、初相,初始化图形数据,生成如演示效果所示的,圆形小球来回摆动的动画效果。你可以通过窗口中的两个按钮,来查看效果:启动有窗体定时器、关闭有窗体定时器。初始化图形数据的代码如下:
this.timer1.Interval = 30;
r1.X = System.Convert.ToInt16(this.panel1.Width / 2 - 30);
r1.Y = System.Convert.ToInt16(this.panel1.Height / 2 - 30);
r1.Width = 60;
r1.Height = 60;
r2 = r1;
x = System.Convert.ToInt16(this.panel1.Width / 2);
- 2022-03-03 11:34:28下载
- 积分:1
-
C# WPF把彩色图片转换为灰度图
C# 把彩色图片转换为灰度图,这是一个基于WPF的C#图像处理程序,图像彩色转换黑白,支持的图像文件格式为:JPeg,Gif,Bmp,etc。
程序主要实现两个功能,一是将彩色转换为索引像素格式、二是将彩色转换为黑白像素格式,对应于窗口中的按钮,可查看对应功能的演示:
将彩色转换为黑白像素格式,核心代码如下:
TransformedBitmap MyRotatedBitmapSource = new TransformedBitmap();
MyRotatedBitmapSource.BeginInit();
MyRotatedBitmapSource.Source = (System.Windows.Media.Imaging.BitmapSource)this.image1.Source;
MyRotatedBitmapSource.Transform = new RotateTransform(270);
MyRotatedBitmapSource.EndInit();
FormatConvertedBitmap MyFormatedBitmap = new FormatConvertedBitmap();
MyFormatedBitmap.BeginInit();
MyFormatedBitmap.Source = MyRotatedBitmapSource;
MyFormatedBitmap.DestinationFormat = PixelFormats.BlackWhite;
MyFormatedBitmap.EndInit();
this.image1.Source = MyFormatedBitmap;
完整源码例子请在本页下载,运行效果截图如下图示。
- 2022-02-01 21:42:57下载
- 积分:1
-
C# 模糊文字 点击按钮文字模糊
C# 制作的模糊文字 点击按钮文字模糊效果,这是基于WPF的一个图像特效,将文字模糊显示,如图所示,运行本程序后,点击窗口中的按钮,即可将按钮中的文字模糊处理。下面来看具体的模糊按钮文字的实现代码:
if (((Button)sender).BitmapEffect != null)
{
((Button)sender).BitmapEffect = null;
}
else
{
Button MyButton = (Button)sender;
var MyBlurEffect = new System.Windows.Media.Effects.BlurBitmapEffect();
MyBlurEffect.Radius = 4;
MyBlurEffect.KernelType = System.Windows.Media.Effects.KernelType.Box;
MyButton.BitmapEffect = MyBlurEffect;
}
- 2022-01-21 04:39:19下载
- 积分:1
-
C# 创建一个泛型接口
C# 创建一个泛型接口的例子,创建一个泛型接口的代码和方法如下:
public interface IGenericInterface {
T CreateInstance(); //接口中调用CreateInstance方法
}
//实现上面泛型接口的泛型类
//派生约束where T : TI(T要继承自TI)
//构造函数约束where T : new()(T可以实例化)
public class Factory : IGenericInterface where T : TI, new()
{
public TI CreateInstance()//创建一个公共方法CreateInstance
{
return new T();
}
}
class Program
{
static void Main(string[] args)
{
//实例化接口
IGenericInterface factory =
Factory();
//输出指定泛型的类型
Console.WriteLine(factory.CreateInstance().GetType().ToString());
Console.ReadLine();
}
}
- 2022-03-11 00:59:39下载
- 积分:1
-
C# 确定字符串末尾是否匹配指定子串
C# 确定字符串末尾是否匹配指定子串,本字符串查询实例具体到只匹配字符串开头和结尾是否有指定的字符串,下面是具体的查询代码编写方法:
private void button1_Click(object sender, EventArgs e)
{//确定字符串开头是否匹配指定子串
string MyFullInfo = "中华人民共和国";
string MyPartInfo="中华";
if(MyFullInfo.StartsWith(MyPartInfo))
MessageBox.Show(MyFullInfo + " 的开头是 " + MyPartInfo, "信息提示", MessageBoxButtons.OK);
else
MessageBox.Show(MyFullInfo + " 的开头不是 " + MyPartInfo, "信息提示", MessageBoxButtons.OK);
}
private void button2_Click(object sender, EventArgs e)
{//确定字符串末尾是否匹配指定子串
string MyFullInfo = "中华人民共和国";
string MyPartInfo = "共和国";
if (MyFullInfo.EndsWith(MyPartInfo))
MessageBox.Show(MyFullInfo + " 的末尾是 " + MyPartInfo, "信息提示", MessageBoxButtons.OK);
else
MessageBox.Show(MyFullInfo + " 的末尾不是 " + MyPartInfo, "信息提示", MessageBoxButtons.OK);
}
- 2022-02-09 23:32:04下载
- 积分:1
-
C# 制作一个顶层显示的窗口-不被其它窗口遮挡
C#设置顶层显示的窗口,让窗口显示在其它窗口的最上面,不被遮挡,这种窗口形式或许大家平时都见到过,特别是一些视频播放器,在播放视频的时候,都可以设置是否顶层显示,本示例就是模拟了这个功能,通过“允许以最顶层方式显示窗口”和“禁止以最顶层方式显示窗口”来模拟启用和关闭顶层窗口的功能,代码貌似简单:
private void button1_Click(object sender, RoutedEventArgs e)
{//允许以最顶层方式显示窗口
this.Topmost = true;
}
private void button2_Click(object sender, RoutedEventArgs e)
{//禁止以最顶层方式显示窗口
this.Topmost = false;
}
- 2023-05-27 13:10:03下载
- 积分:1