单元格赋值
DataGridView1.Rows.Add()'首先新增一空行
方法一: DataGridView1.Item("Column1", 1).Value = "adsfsdf" '其中Column1是列名称(列的name属性值)并非列的text值,1代表第1行
方法二:
DataGridView1.Rows(1).Cells(0).Value = "Test” '其中1是第一行,0是第0个单元格,datagridview中起始行号和列号及单完格都从0开始计
删除行
方法一:
DataGridView1.Rows.Remove(DataGridView1.CurrentRow)'删除当前光标所在行
DataGridView1.Rows.Remove(DataGridView1.Rows(DataGridView1.Rows.Count - 1))'删除最后一行
DataGridView1.Rows.Clear()'删除所有行
方法二:
tnck = "料件进口合同表"
Dim ds As New DataSet()
Dim sql As String
objSqlConnection.Open()
sql = "delete from " + tnck + " where 序号='" & DataGridView1.CurrentRow.Cells("序号").Value & "'"
Dim sda As SqlDataAdapter
sda = New SqlDataAdapter(sql, objSqlConnection)
ds = New DataSet
sda.Fill(ds, tnck)
DataGridView1.DataSource = ds.Tables(tnck)
objSqlConnection.Close()
MsgBox("删除完成!")
方法三
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
r = e.RowIndex; //获取当前选择行的行号
c = dataGridView1.Rows[r].Cells[1].Value.ToString(); //根据当前选择行的行号匹配得到(Cells[0])0列的数据
}
完整的一个例子
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace led
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSend_Click(object sender, EventArgs e)
{
if (this.serialPort1.IsOpen)
{
this.serialPort1.Close();
}
this.serialPort1.PortName = this.cbSerial.Text;
this.serialPort1.Open();
System.Drawing.Bitmap bmp = new Bitmap(this.tbCurrent.Width, this.tbCurrent.Height);
this.tbCurrent.DrawToBitmap(bmp,new Rectangle(0,0,bmp.Width,bmp.Height));
Until.SendBmp(this.serialPort1, bmp);
}
private void Form1_Load(object sender, EventArgs e)
{
FillAllData(0);
}
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
this.tbContent.Text = this.dgvData["content", e.RowIndex].Value.ToString();
this.tbCurrent.Text = this.dgvData["content", e.RowIndex].Value.ToString();
this.tbCurrentSortid.Text = this.dgvData["sortid", e.RowIndex].Value.ToString();
id = e.RowIndex;
}
private int id;
private void button2_Click(object sender, EventArgs e)
{
if (this.tbContent.Text == "")
{
MessageBox.Show("输入内容不能为空");
return;
}
int count = Until.GetLineCount(tbContent);
string[] temp = new string[50];
for (int i = 0; i < count; i++)
{
DB.insertLED(Until.GetLine(tbContent, i));
}
FillAllData(this.dgvData.Rows.Count);
datachange = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (this.tbContent.Text == "")
{
MessageBox.Show("输入内容不能为空");
return;
}
int count = Until.GetLineCount(tbContent);
if (count > 1)
{
MessageBox.Show("修改只限于1行,不能多于一行");
return;
}
if (DB.updateLEDcontent(tbContent.Text,dgvData["id",id].Value.ToString()) > 0)
{
FillAllData(id);
datachange = true;
}
}
private void FillAllData(int selectid)
{
this.dgvData.DataSource = DB.selectLEDcontent();
this.dgvData.Columns["id"].Visible = false;
this.dgvData.Columns["content"].HeaderText = "显示内容";
this.dgvData.Columns["content"].Width = 300;
this.dgvData.Columns["sortid"].HeaderText = "序号";
this.dgvData.Columns["inputtime"].HeaderText = "最近修改时间";
this.dgvData.Columns["inputtime"].Width = 200;
if (selectid > dgvData.Rows.Count - 1)
{
selectid = dgvData.Rows.Count - 1;
}
this.dgvData.CurrentCell = this.dgvData.Rows[selectid].Cells["content"];
}
private void button4_Click(object sender, EventArgs e)
{
DB.updateLEDsortid(dgvData["id", id].Value.ToString(), this.tbCurrentSortid.Text, this.nudNewSortid.Value.ToString());
FillAllData((int)this.nudNewSortid.Value);
datachange = true;
}
private void button3_Click(object sender, EventArgs e)
{
DB.updateLEDsortidlast(dgvData["id", id].Value.ToString(), this.tbCurrentSortid.Text);
FillAllData(this.dgvData.Rows.Count);
datachange = true;
}
private void button5_Click(object sender, EventArgs e)
{
this.timer1.Interval = (int)this.nudTime.Value * 1000;
LoadData();
this.timer1.Enabled = true;
}
private bool datachange = false;
private string[] datatemp;
private int datan = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (datachange)
{
LoadData();
datan = 0;
datachange = false;
}
try
{
this.tbCurrent.Text = datatemp[datan];
}
catch
{
datan = 0;
this.tbCurrent.Text = datatemp[datan];
}
datan++;
//SendBySPort(this.textBox1);
}
private void LoadData()
{
datachange = true;
datan = 0;
DataTable dt = DB.selectLEDcontent();
int i = 0;
datatemp = new string[dt.Rows.Count];
foreach (DataRow r in dt.Rows)
{
datatemp[i] = r["content"].ToString();
i++;
}
}
private void SendBySPort(TextBox tb)
{
if (this.serialPort1.IsOpen)
{
this.serialPort1.Close();
}
this.serialPort1.PortName = this.cbSerial.Text;
this.serialPort1.Open();
System.Drawing.Bitmap bmp = new Bitmap(this.tbCurrent.Width, this.tbCurrent.Height);
tb.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
Until.SendBmp(this.serialPort1, bmp);
}
private void button6_Click(object sender, EventArgs e)
{
this.timer1.Enabled = false;
}
private void btnDeleteContent_Click(object sender, EventArgs e)
{
DB.deleteLED(dgvData["id", id].Value.ToString());
FillAllData(id);
}
}
}