-
C#读取数据库内容并在dataGridView中显示
C#从数据库中读取内容并显示在dataGridView中,这似乎是一个很实用的功能,在数据库应用的时候,我们都要通过dataGridView来显示数据,这个例子可帮助初学者很好的掌握此功能的具体实现,一些代码片段分享如下:
private void button1_Click(object sender, EventArgs e)
{
//实例化SqlConnection变量conn,连接数据库
conn = new SqlConnection("server=.;database=db_15;uid=sa;pwd=");
//实例化SqlDataAdapter对象
SqlDataAdapter sda = new SqlDataAdapter("select * from tb_emp", conn);
DataSet ds = new DataSet(); //实例化DataSet对象
sda.Fill(ds);//使用SqlDataAdapter对象的Fill方法填充DataSet
dataGridView1.DataSource = ds.Tables[0];//设置dataGridView1控件的数据源
dataGridView1.RowHeadersVisible = false;//禁止显示行标题
//使用for循环设置控件的列宽
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
dataGridView1.Columns[i].Width = 84;
}
button1.Enabled = false;//禁用按钮
dataGridView1.Columns[0].ReadOnly = true;//将控件设置为只读
}
private DataTable dbconn(string strSql)//建立一个DataTable类型的方法
{
this.adapter = new SqlDataAdapter(strSql, conn);//实例化SqlDataAdapter对
- 2022-07-24 21:44:03下载
- 积分:1
-
C# FTP客户端模块 上传下载文件显示进度
C# FTP客户端模块 上传下载文件显示进度,本示例可通过HTTP、FTP下载文件,可通过FTP上传文件,请设定好服务器IP地址再测试,进度条在窗口的最上方。
percent = (float)totalDownloadedByte / (float)totalBytes * 100;
label1.Text = "当前补丁下载进度" + percent.ToString() + "%";
Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);//用户,密码
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;//向服务器发出下载请求命令
reqFTP.ContentLength = finfo.Length;//为request指定上传文件的大小
- 2022-03-23 13:46:25下载
- 积分:1
-
C# 启动和关闭无窗体定时器
C# 启动和关闭无窗体定时器,单击窗体上对应的按钮,即可完成无窗体定时器的启动和关闭功能,核心代码为:
private void button1_Click(object sender, EventArgs e)
{//启动无窗体定时器
var MyClass = new MyThreadClass();
MyTimer = new System.Threading.Timer(MyClass.MyCallBackMethod, MyClass, 5000, 2000);
}
private void button2_Click(object sender, EventArgs e)
{//关闭无窗体定时器
MyTimer.Dispose();
}
- 2022-04-09 03:53: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# 获取计算机上存在的时区信息,并把北京时间转换为目标时区时间。
- 2022-03-26 01:29:39下载
- 积分:1
-
C# 向StatusBar状态栏控件中添加窗格面板
C# 向StatusBar状态栏控件中添加窗格面板及文字,也就是把窗口的状态栏分栏,分隔成若干个小区域,显示不同的信息,比如文字提示或图标等。
向StatusBar控件添加窗格面板
StatusBar statusBar1=new StatusBar();
statusBar1.Panels.Add("中华人民共和国");
statusBar1.Panels.Add("重庆市");
statusBar1.Panels.Add("罗斌");
statusBar1.Panels[0].AutoSize = StatusBarPanelAutoSize.Contents;
statusBar1.Panels[1].AutoSize = StatusBarPanelAutoSize.Spring;
statusBar1.Panels[2].AutoSize = StatusBarPanelAutoSize.Contents;
statusBar1.Panels[0].BorderStyle =StatusBarPanelBorderStyle.Raised;
statusBar1.Panels[1].BorderStyle = StatusBarPanelBorderStyle.Sunken;
statusBar1.Panels[2].BorderStyle = StatusBarPanelBorderStyle.Raised;
statusBar1.Panels[2].Icon = new System.Drawing.Icon( @"Error.ico");
statusBar1.ShowPanels = true;
this.Controls.Add(statusBar1);
- 2022-05-17 04:53:06下载
- 积分: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#制作软件启动时的欢迎界面,开始画面,在软件被打开时最先显示的一个窗口效果,在本例中是直接调用一张图片来显示,但是具体的实现,比如图片显示的位置 、显示的时间长短等,用到的定时器,需要控制好,本实例代码就是向大家展示如何进行这些控制,部分代码为:
private void Form1_Load(object sender, EventArgs e)
{//启动窗体
Form2 MySplashForm = new Form2();
MySplashForm.ShowDialog();
}
private void Form2_Load(object sender, EventArgs e)
{//设置启动窗体
this.FormBorderStyle = FormBorderStyle.None;
this.BackgroundImage = Image.FromFile("test.jpg");
this.timer1.Start();
this.timer1.Interval = 10000;
}
private void timer1_Tick(object sender, EventArgs e)
{//关闭启动窗体
this.Close();
}
定时器控制:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{//关闭定时器
this.timer1.Stop();
}
- 2022-01-22 15:57:51下载
- 积分:1
-
C# 自定义组件的小例子及源代码
C# 自定义组件的小例子及源代码,包含自定义的UserControl组件和应用该组件的MainFrame主窗体程序,需要在工具箱里右键-》选项-》.Net组件注册一下就能用了。主要通过将xml绑定到TreeView控件完成菜单导航功能,可以在同一窗体中导航不同panel,也可以导航多窗口,需要添加新菜单,只需配置Menu.xml,Menu.xml中显示treeView树形菜单中的各节点数据,将除菜单外所有panel设置为不显示。
- 2022-11-10 22:05:03下载
- 积分:1
-
Visual C# 多线程异步抓取网页 网络爬虫控制台程序模拟
Visual C# 多线程异步抓取网页 网络爬虫控制台程序模拟,程序中定义了一个网页类爬虫程序,通过此程序可获得本网页的网址、网页标题、网页的所有链接信息,只读方式,并且返回网页的全部纯文本信息,获得本网页的大小,从HTML代码中分析出链接信息,从一段HTML文本中提取出一定字数的纯文本,提取网页中一定字数的纯文本,包括链接文字,从本网页的链接中提取一定数量的链接,该链接的URL满足某正则式,从本网页的链接中提取一定数量的链接,该链接的文字满足某正则式等等。
- 2022-05-18 08:50:12下载
- 积分:1