第一个 加上行号 如果可以的话 麻烦给个代码 ;
第二 个 创建了一个 datagridview 然后 隐藏了 其中数据库主键id列 如何 实现 鼠标点在form表格中的单元格 读取单元格的 id。
第三个 实现鼠标点在某行 某行中的颜色和其他的行的不一样
第二 个 创建了一个 datagridview 然后 隐藏了 其中数据库主键id列 如何 实现 鼠标点在form表格中的单元格 读取单元格的 id。
第三个 实现鼠标点在某行 某行中的颜色和其他的行的不一样
解决方案 »
- C#中将mysql中的datetime字段赋值给DateTimePicker控件
- CSDN 的开发人员太................
- 今天是入伍5周年纪念日,散分!
- 请问怎么用C#保存word之类的文件到Access并再读取出来啊!
- 懂C#的进来讨论一下!
- C#对WORD操作时页眉处理
- 急救!!!!把一段VB代码转为C#!!!各位大虾帮帮忙,急!!在线等!!!!!!!
- 为什么我用VS.NET C#写的软件只能在windows2003上运行?
- 关于TREEVIEW和dataGrid相关联的问题。急~~在线等!!
- Unity连接了MySQL数据库代码调试时出现VS问题
- textbox如何控制禁止输入半角,只允许输入全角~!
- 写了一个软件.如何才能让它一直运行?
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
{
e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
}
}
private void DataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex < 0 && e.RowIndex >= 0)
{
e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
Rectangle indexRect = e.CellBounds;
indexRect.Inflate(-2, -2);
TextRenderer.DrawText(e.Graphics,
(e.RowIndex + 1).ToString(),
e.CellStyle.Font,
indexRect,
e.CellStyle.ForeColor,
TextFormatFlags.Right | TextFormatFlags.VerticalCenter);
e.Handled = true;
}
} }
问题二:在点击事件中直接读取。
问题三:在CellClick事件中循环设置(i为各列的Index),如:
DataGridView1.Rows(e.RowIndex).Cells(i).Style.BackColor = Color.White
用这个读取的话 因为第一列 的id被隐藏了,所以读取的是表中第二列的数据 不是主键,
那怎么读取啊 ?
private void DataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex < 0 && e.RowIndex >= 0)
{
e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
Rectangle indexRect = e.CellBounds;
indexRect.Inflate(-2, -2);
TextRenderer.DrawText(e.Graphics,
(e.RowIndex + 1).ToString(),
e.CellStyle.Font,
indexRect,
e.CellStyle.ForeColor,
TextFormatFlags.Right | TextFormatFlags.VerticalCenter);
e.Handled = true;
}
} }
问题二:在点击事件中直接读取。
问题三:在CellClick事件中循环设置(i为各列的Index),如:
DataGridView1.Rows(e.RowIndex).Cells(i).Style.BackColor = Color.White
http://bingning.net/free/source/datagridview/drawrownumber.html
第二个问题可以读取相应DataSet中的ID,类似你的办法。
首先,表格的数据源用bindingSource
其次,在bindingSource的PositionChanged事件中调用下面代码得到DataRow行,然后就可以得到你想要的数据了DataRow["id"].
if (bindingSource1.Current != null && bindingSource1.Current as DataRowView!=null)
Dr = (bindingSource1.Current as DataRowView).Row as DataRow;http://www.mystruggle.com.cn
bindingSource1.DataSource = dataSet2;
bindingSource1.DataMember = "main_666666";
dataGridView1.DataSource = bindingSource1;
bindingSource1.PositionChanged += ((object sender1, EventArgs e1) =>
{
if (bindingSource1.Current != null && bindingSource1.Current as DataRowView != null)
{ Dr = (bindingSource1.Current as DataRowView).Row as DataRow;
textBox1.Text = Dr["id"].ToString();} });
}或
sqlDataAdapter_create.Fill(dataSet2, "main_666666");
bindingSource1.DataSource = dataSet2;
bindingSource1.DataMember = "main_666666";
dataGridView1.DataSource = bindingSource1;
bindingSource1.PositionChanged +=new EventHandler(bindingSource1_PositionChanged);
} private void bindingSource1_PositionChanged(object sender, EventArgs e)
{
if (bindingSource1.Current != null && bindingSource1.Current as DataRowView != null)
{
Dr = (bindingSource1.Current as DataRowView).Row as DataRow;
textBox1.Text = Dr["id"].ToString();
}
}http://www.mybuffet.cn
private void bindingSource1_PositionChanged(object sender, EventArgs e)
{
if (bindingSource1.Current != null && bindingSource1.Current as DataRowView != null)
{
Dr = (bindingSource1.Current as DataRowView).Row as DataRow;
textBox1.Text = Dr["id"].ToString();
}
}
这段代码我明白了
我想问一下
sqlDataAdapter_create.Fill(dataSet2, "main_666666");
bindingSource1.DataSource = dataSet2;
bindingSource1.DataMember = "main_666666";
dataGridView1.DataSource = bindingSource1;
bindingSource1.PositionChanged += ((object sender1, EventArgs e1) =>
{
if (bindingSource1.Current != null && bindingSource1.Current as DataRowView != null)
{ Dr = (bindingSource1.Current as DataRowView).Row as DataRow;
textBox1.Text = Dr["id"].ToString();} });
}
这段代码什么意思 为什么我测试的时候提示有错误
protected override void OnRowsAdded(System.Windows.Forms.DataGridViewRowsAddedEventArgs e)
{
base.OnRowsAdded(e);
if (Rows.Count > 0)
{ for (int i = 0; i < this.Rows.Count; i++)
{
this.Rows[i].HeaderCell.Value = (i + 1).ToString();
} }
}
protected override void OnColumnHeaderMouseClick(System.Windows.Forms.DataGridViewCellMouseEventArgs e)
{
base.OnColumnHeaderMouseClick(e);
if (Rows.Count > 0)
{ for (int i = 0; i < this.Rows.Count; i++)
{
this.Rows[i].HeaderCell.Value = (i + 1).ToString();
}
}
}
如果你没有用绑定来做的话
可以考虑把列ID或其他有用但是不必显示的数据存在dataRow.tag中
是不是我哪里操作不对啊 ?
定义一个数据表,DATATABLE,用其属性加序号。
你可以只用
sqlDataAdapter_create.Fill(dataSet2, "main_666666");
bindingSource1.DataSource = dataSet2;
bindingSource1.DataMember = "main_666666";
dataGridView1.DataSource = bindingSource1;
bindingSource1.PositionChanged +=new EventHandler(bindingSource1_PositionChanged);
} private void bindingSource1_PositionChanged(object sender, EventArgs e)
{
if (bindingSource1.Current != null && bindingSource1.Current as DataRowView != null)
{
Dr = (bindingSource1.Current as DataRowView).Row as DataRow;
textBox1.Text = Dr["id"].ToString();
}
} 另一段只是另一种写法:lambda expression 只有在3.0以上才可以,还要引用一些单元。http://www.mybuffet.cn
namespace cs
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent(); }
private DataRow Dr; private void Form3_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“rlzyDataSet1.p_basicinfo”中。您可以根据需要移动或移除它。
this.p_basicinfoTableAdapter.Fill(this.rlzyDataSet1.p_basicinfo); //填充数据集
pbasicinfoBindingSource.DataSource = rlzyDataSet1; //pbasicinfoBindingSource用rlzyDataSet1作数据源
pbasicinfoBindingSource.DataMember = "p_basicinfo";
dataGridView1.DataSource = pbasicinfoBindingSource; //用pbasicinfoBindingSource作数据源
pbasicinfoBindingSource.PositionChanged += new EventHandler(bindingSource1_PositionChanged); //加事件PositionChanged }
private void bindingSource1_PositionChanged(object sender, EventArgs e)
{
if (pbasicinfoBindingSource.Current != null && pbasicinfoBindingSource.Current as DataRowView != null)
//pbasicinfoBindingSource.Current 是个 DataRowView 类型指示当前行
{
Dr = (pbasicinfoBindingSource.Current as DataRowView).Row as DataRow; //把当前行又转换为DataRow 行集
textBox1.Text = Dr["id"].ToString();//读id字段
}
} }
}
int maxhh = 0; private void Form1_Load(object sender, EventArgs e)
{
//“hh” 为行号 ,table要手动加入列,包括"hh"
sqlDataAdapter1.Fill(dataSet1.Tables[0] );
int i=1;
dataSet1.Tables[0].Columns["hh"].ReadOnly = false;
foreach (DataRow r in dataSet1.Tables[0].Rows)
{
r["hh"] = i;
maxhh = i;
i++;
}
dataSet1.Tables[0].Columns["hh"].ReadOnly = true ;
dataSet1.Tables[0].TableNewRow += new DataTableNewRowEventHandler(Form1_TableNewRow);
dataSet1.Tables[0].RowDeleted += new DataRowChangeEventHandler(Form1_RowDeleted); } void Form1_RowDeleted(object sender, DataRowChangeEventArgs e)
{
//重整行号,使行号连续
int i = 1;
dataSet1.Tables[0].Columns["hh"].ReadOnly = false;
foreach (DataRow r in dataSet1.Tables[0].Rows)
{
if (r.RowState != DataRowState.Deleted)
{
r["hh"] = i;
maxhh = i;
i++;
}
}
dataSet1.Tables[0].Columns["hh"].ReadOnly = true; } void Form1_TableNewRow(object sender, DataTableNewRowEventArgs e)
{
dataSet1.Tables[0].Columns["hh"].ReadOnly = false;
maxhh++;
e.Row["hh"] = maxhh;
dataSet1.Tables[0].Columns["hh"].ReadOnly = true;
}http://www.mystruggle.com.cn