dgvconfirm.Columns["罐号"].Frozen = true;//锁定列的位置
dgv_bk.Focus()
dgv_bk.CurrentCell = dgv_bk.Rows[0].Cells[0] //焦点
数据插入
private int rowMax = 0;
dgSplitData.CurrentCell = dgSplitData[0, 0];
string tempStrSQL;
//rowMax为当前DataGridView中最大行号
for (int i = rowMax; rowMax < dgSplitData.Rows.Count; rowMax++)
{
if (this.dgSplitData.Rows[rowMax].Cells[0].Value == null)
{
break;
}
else
{
string cargoCoName = dgSplitData.Rows[rowMax].Cells[0].Value.ToString().Trim();
string splitWeight = dgSplitData.Rows[rowMax].Cells[1].Value.ToString().Trim();
string shipName = dgSplitData.Rows[rowMax].Cells["ShipName"].Value.ToString().Trim();
string column3 = dgSplitData.Rows[rowMax].Cells["Column3"].Value.ToString().Trim();
string remark = dgSplitData.Rows[rowMax].Cells["Remark"].Value.ToString().Trim();
string sendValues = "('" + cargoCoName + "','" + splitWeight + "','" + shipName + "','" + column3 + "' ,'" + remark + "')";
tempStrSQL = "insert into t_SplitData (CargoCompany,Weight,ShipName,Commodity,Remark) values" + sendValues; App.UpdateDataBase(tempStrSQL);
}
数据读取
注意属性:AutoSizeColumnsMode=Fill DOCK=Fill 必须有列头存在
private void 显示数据ToolStripMenuItem_Click(object sender, EventArgs e)
{
sql = "select * from t_user";
using (SqlConnection con = new SqlConnection("Data Source=xxxx;Database=xx;user id=xx;password=xxxxx"))
{
using (SqlCommand cmd = new SqlCommand(sql,con))
{
cmd.CommandType = CommandType.Text;//系统默认为文本模式可不用写
con.Open();
SqlDataReader reader= cmd.ExecuteReader();
while (reader.Read())
{
dataGridView1.Rows.Add(reader.GetInt32(0).ToString(), reader.GetString(1), reader.GetString(2)); //在行中加载各列的值
//dataGridView1.Rows.Add(reader["数据库列名"].Tostring(),reader["数据库列名"]。Tostring());
}
}
}
}
public void GetData() //手动添加列
{
dataGridView1.Columns.Clear();
dataGridView1.Rows.Clear();
dataGridView1.Columns.Add("userid", "用户编号");
dataGridView1.Columns.Add("userName", "登录用户名");
dataGridView1.Columns.Add("userPassword", "登录密码");
}
//数据绑定
public void GetDataAdapter()
{
sql = "select userid as'用户编号',userName as'登录用户名',userPassword as'登录密码' from t_user";
using (SqlConnection con = new SqlConnection("Data Source=XXXXX;Database=XXXXX;user id=XX;password=XXXX"))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(sql,con))
{
con.Open();
DataSet dt = new DataSet();
adapter.Fill(dt);
dataGridView1.DataSource = dt.Tables[0];
}
}
}
//获取选中的单元格
public void GetCell()
{
string msg = String.Format("第{0}行,第{1}列,单元格的值{2}",dataGridView1.CurrentCell.RowIndex+1,dataGridView1.CurrentCell.ColumnIndex+1,dataGridView1.CurrentCell.Value);
label1.Text = "" + msg;
}
//选中的单元格颜色变换
public void GetRowColor()
{
dataGridView1.SelectionMode=DataGRIDViewSelectionMode.FullRowSelect;//选中一行
dataGridView1.DefaultCellStyle.SelectionBackColor=Color.Red;
}
//更新数据
public void DataUpdate()
{
DataTable ds=dataGridView1.DataSource as DataTable;
if (ds != null)
{
using (SqlConnection con = new SqlConnection("Data Source=10.228.18.149;Database=web1;user id=sa;password=zhang.1217"))
{
string updateSql = "update t_user set userName=@userName,userpassword=@userpassword where userid=@userid ";
SqlDataAdapter adapter = new SqlDataAdapter();
using (SqlCommand cmdUpdate = new SqlCommand(updateSql, con))
{
cmdUpdate.Parameters.Add("@userName", SqlDbType.VarChar, 50, "userName");
cmdUpdate.Parameters.Add("@userid", SqlDbType.VarChar, 50, "userid");
cmdUpdate.Parameters.Add("@userpassword", SqlDbType.VarChar, 50, "userpassword");
adapter.UpdateCommand = cmdUpdate;
}
string insertSql = "insert into t_user (userName,userpassword) values(@userName,@userpassword)";
using (SqlCommand cmdInsert = new SqlCommand(insertSql, con))
{
cmdInsert.Parameters.Add("@userName", SqlDbType.VarChar, 50, "userName");
cmdInsert.Parameters.Add("@userpassword", SqlDbType.VarChar, 50, "userpassword");
adapter.InsertCommand = cmdInsert;
}
string deleteSql = "delete from t_user where userid=@userid";
using (SqlCommand deleteCmd= new SqlCommand(deleteSql, con))
{
deleteCmd.Parameters.Add("@userid", SqlDbType.Int, 50, "userid");
adapter.DeleteCommand = deleteCmd;
}
con.Open();
adapter.Update(ds);
}
GetDataAdapter();
}
}
//添加行
必须有列头存在,才可以
ataGridViewRow row = new DataGridViewRow();
DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
//textboxcell.Value = "aaa";
row.Cells.Add(textboxcell);
//DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell();
//row.Cells.Add(comboxcell);
dataGridView1.Rows.Add(row);
//获得列头的方法
Private void GetFilderColumn()
{
suing(SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=数据库名称;User ID=sa;password="))
{
SqlCommond cmd = new SqlCommond("select * ferom 表名",con);
con.open();
SqlDataReader reader =cmd.SqlDataReader();
DataTabel dt = new DataTabel();
for(int i = 0 ; i<reader.FieldCount;i++)
{
DataColumn dc = new DataColumn(); //获得列名
dc.DataType=reader.GetFieldType(i);//列名类型
dt.Colums.add(dc);
DataGridView.DataSource=dt;
}
}
}