C# winform ComboBox添加 +DataGridView绑定,右键菜单,内部编辑,内容清空+多个DataTable存入,合并为一个,并统计重复+账号密码校验+获取当前数据库所有表名
初学总结:一些简单内容ComboBox 添加 //清除ComboBox
CmbName.Items.Clear();
//ComboBox添加空白列
CmbName.Items.Add("");
//查询Sql
DataSet TableName = DbHelper.Query(Sql);
//遍历循环TableName将值添加到CmbName.Items
foreach (DataRow dr in TableName.Tables[0].Rows)
{
CmbName.Items.Add(dr[""]);
}
DataGridView
//获取表
DataSet TableName = DbHelper.Query(Sql);
//绑定表
DataName.DataSource = TableName.Tables[0];
DataGridView 右键菜单
调用contextMenuStrip1控件,编辑控件内容项之后
引用事件DataReview_CellMouseDown
private void DataReview_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0)
{
DataReview.ClearSelection();
DataReview.Rows[e.RowIndex].Selected = true;
DataReview.CurrentCell = DataReview.Rows[e.RowIndex].Cells[e.ColumnIndex];
MenuStripReview.Show(MousePosition.X, MousePosition.Y);
}
}
}
DataGridView中编辑
调用事件DataReview_CellEndEdit
调用事件DataReview_CellBeginEdit
private void DataReview_CellEndEdit(object sender, DataGridViewCellEventArgs e)//DataGridView中编辑
{
if (DataReview.Rows.Count > 0)//判断选中行
{
string GroupName = "";
GroupName = DataReview.Rows[e.RowIndex].Cells[1].Value.ToString();
//判断编辑前后的值是否一样
if (Object.Equals(cellTempValue, DataReview.Rows[e.RowIndex].Cells[e.ColumnIndex].Value))
{
//如果没有修改,则返回
return;
}
else
{
DataSet GrUp = DbHelperMySQL.Query("select * From review where cname='" + GroupName + "'");
if (GrUp.Tables.Count > 0 && GrUp.Tables[0].Rows.Count > 0)
{
DataReview.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = cellTempValue;
}
else
{
string id = DataReview.CurrentRow.Cells["Autoid"].Value.ToString();
string sql = String.Format("update review set cname='" + GroupName + "' where Autoid='" + id + "'");
DbHelperMySQL.ExecuteSql(sql);
}
}
}
}
private void DataReview_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
cellTempValue = DataReview.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
}
DataTable基本操作
//遍历多个DataTable存入DataSet
//Table.Tables.Add(Name.Copy());
//遍历多个DataTable合并
Table.Merge(Name, true, MissingSchemaAction.AddWithKey);
//复制DataTable结构
TableName = Table.Clone();
//手动创建列
TableName.Columns.Add("号码", Type.GetType("System.String"));
TableName.Columns["号码"].SetOrdinal(0);
TableName.Columns.Add("频次", Type.GetType("System.String"));
TableName.Columns["频次"].SetOrdinal(1);
//DataTable中 Group By Count (DataTable实现统计效果)
if (Table.Rows.Count > 0)
{
DataTable dtName = Table.DefaultView.ToTable(true, "号码");
for (int i = 0; i < dtName.Rows.Count; i++)
{
DataRow[] rows = Table.Select("号码='" + dtName.Rows[i][0] + "' ");
//temp用来存储筛选出来的数据
DataTable temp = TableName.Clone();
foreach (DataRow row in rows)
{
temp.Rows.Add(row.ItemArray);
}
DataRow dr = TableName.NewRow();
dr[0] = dtName.Rows[i][0].ToString();
dr[1] = temp.Compute("count(号码)", "");
TableName.Rows.Add(dr);
}
DataName.DataSource = TableName;}
账号密码校验
public void Account_password_verification(TextBox txtUserName, TextBox txtPwd)//账号密码校验
{
if (txtUserName.Text != "" && txtPwd.Text != "")
{
DataSet User = DbHelperMySQL.Query("select * from User where User='" + txtUserName.Text + "'");
if (User.Tables.Count > 0)
{
if (User.Tables[0].Rows.Count > 0)
{
DataSet UserAndPassWord = DbHelperMySQL.Query("select * from User where User='" + txtUserName.Text + "' and PassWord='" + txtPwd.Text + "'");
if (UserAndPassWord.Tables[0].Rows.Count > 0)
{
}
else
{
CommonUtil.MsgBoxErr("密码错误!", "错误:");
}
}
else
{
CommonUtil.MsgBoxErr("账号不存在!", "错误:");
}
}
}
else
{
CommonUtil.MsgBoxErr("账号密码不能为空!", "错误:");
}
}
DataGridView内容清空
public void DataGridViewClear(DataGridView DataName)//清空DataGridView
{
if (DataName.Rows.Count > 0)
{
//注:DataGridView为DataSource绑定
DataTable dt = (DataTable)DataName.DataSource;
dt.Rows.Clear();
dt.Columns.Clear();
DataName.DataSource = dt;
}
}
获取数据库所有表
public void GetServerTableName()//获取数据库所有表
{
//SQL select name from sys.tables
DataSet TableName = DBHelper.Query("SELECT SysObjects.name AS Tablename FROM sysobjects WHERE xtype = 'U'");
foreach (DataRow dr in TableName.Tables[0].Rows)
{
CmbExout.Items.Add(dr["TableName"]);
}
}
DataGridView中价格计算
调用事件dgv1_CellEndEdit
private void dgv1_CellEndEdit(object sender, DataGridViewCellEventArgs e)//总价计算
{
if (dgv1.SelectedRows.Count > 1)
{
for (int i = 0; i < dgv1.Rows.Count; i++)
{
if (int.Parse(dgv1.Rows[e.RowIndex].Cells[13].Value.ToString()) > 0)
{
//单价
float Num = int.Parse(dgv1.Rows[e.RowIndex].Cells[8].Value.ToString());
//数量
float Price = int.Parse(dgv1.Rows[e.RowIndex].Cells[9].Value.ToString());
string AllPrice = (Num * Price).ToString();
//行最高价
dgv1.Rows[e.RowIndex].Cells[13].Value = AllPrice;
}
}
}
}
- DataSet&DataTable如何绑定下拉列表控件获取不重复的值,如何添加一列自定义文字,并在下拉列表控件中显示自定义文字[winform学习实例]
- C#编写的winform程序绑定comboBox成功,添加一个默认的文字选项"请选择"
- C# Winform中的ComboBox控件绑定数据库项目作为列表内容
- C# winform datagridview中如何实现鼠标右键点击一行数据出现一个带有删除的菜单,并能执行删除操作?
- c#(winform)中自定义ListItem类方便ComboBox和ListBox添加项完全解决(DropDownList 和Combox 数据绑定的常用方法) .
- c#+Winform实现自定义的“复制、粘贴”右键快捷菜单,多个控件共享使用一个右键菜单
- C# winform treeview添加右键菜单并选中节点
- c#(winform)中ComboBox添加Key/Value项、获取选中项、根据Key
- c#(winform)中ComboBox和ListBox添加项,写一个用户控件
- c#,winform,contextmenutrip,treeview,右键快捷菜单,定位问题,当前节点
- C# WINFORM中的combobox.items.add实现像web开发那样,添加显示内容text和实际value值
- 使用所见即所得文本编辑器编辑文本存入数据库后通过ajax获取服务器json_encode的数据到前台,文本内容上边的html标签不解析
- C#之WinForm基础 新建一个不可编辑的comboBox
- win7,win10右键添加“获取管理员的所有权限”的菜单
- winform中DataGridView添加ComboBox的最终解决方案(点击ComboBox默认显示当前行的内容)
- c#+Winform实现自定义的“复制、粘贴”右键快捷菜单,多个控件共享使用一个右键菜单。
- 如何在TabControl中响应右键鼠标(添加一个ContextMenu就可以了),如何让在tab标签处右键菜单和在内容处右键菜单显示不同的菜单?
- 怎样将一个数据库里的所有数据表的名字绑定到ComboBox中
- C#一个DataSet中存入多个DataTable的方法
- C# winform treeview node添加右键菜单并选中节点