C# datagrid 怎么当前列获取行中的数据 (不是dataGridView)

比如 打印 怎么在后台获取到(蓝牌,冀B663f2)选择行中的 数据值
2024-05-07 15:57:17
推荐回答(3个)
回答1:

估计“打印”是一个LinkButton,你把该Linkbutton的CommandName设置为print,然后在该DataGrid控件的事件里找到ItemCommand,双击进入该事件。然后:

if (e.CommandName == "print")
            {
                string id = e.Item.Cells[0].Text;
                string carNum = e.Item.Cells[1].Text;
                string carType = e.Item.Cells[2].Text;
                string address = e.Item.Cells[3].Text;
                string time = e.Item.Cells[4].Text;
                string action = e.Item.Cells[5].Text;
                string isPrint = e.Item.Cells[6].Text;

                Page.ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("", id, carNum, carType, address, time, action, isPrint));
            }

这样就获取到了~~~~~~~

回答2:

根据SelectedItem属性找到选中行的的id,然后根据id查找datagrid绑定的DataSource中对应id的数据,然后打印出来

回答3:

if (this.dataGridView3.SelectedRows.Count > 0)
{
DialogResult dr = MessageBox.Show("确定打印选中的记录 ", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
try
{
foreach (DataGridViewRow row in this.dataGridView3.SelectedRows) //遍历所选中的dataGridView记录行
{
number = row.Cells[2].Value.ToString();//获取车型取打印行中第三列的值
ids = row.Cells[1].Value.ToString();//获取车牌号取打印行中第二列的值
//执行打印操作 下面写打印代码将获取的信息打印出来
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "提示");
}
}
else
{
return;
}
}