我有一个从SQL Server调用的DataGridView,并以列和行的形式显示抓取的信息。在最后一行中,我让它作为一个按钮显示文本(..for),当我抓取信息时,它只显示视图,而不是像它应该的那样将数据从MySQL加载到文本框中
我尝试了很多方法,但都不能使用列/行中的datagridview按钮。
private void frmRepoPremier_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
string query = "select * from Repos";
MySqlConnection sqlConnection = new MySqlConnection(MyConnectionString);
MySqlCommand sqlCommand = new MySqlCommand(query, sqlConnection);
MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(sqlCommand);
sqlConnection.Open();
sqlDataAdapter.Fill(ds);
sqlConnection.Close();
dgvBuildings.DataSource = ds.Tables[0];
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
dgvBuildings.Columns.Add(btn);
btn.HeaderText = "Photo";
btn.Text = "View";
btn.Name = "btn";
btn.UseColumnTextForButtonValue = true; }
private void dgvBuildings_CellClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
{
//TRYING TO LOAD TO TEXTBOX WHEN ROW IS CLICKED AND SHOW VALUE OF PHOTO COLUMN FROM DATABASE IN SQL
//TRYING HERE!
textBox1.Text = dgvBuildings.Rows[e.RowIndex].Cells[11].ToString();
//TRYING HERE!
// for (int x = 0; x <= dgvBuildings.Rows.Count - 1; x++)
// {
// textBox1.Text = dgvBuildings.Rows[x].Cells[11].ToString();
// }
//OPEN IMAGE FROM BUTTON
// System.Diagnostics.Process.Start(textBox1.Text);
}
}
当我点击查看按钮时,我会让它打开浏览器或带有图像的PictureBox (图像链接存储在sql server中),并且应该在文本框中读取它。
发布于 2019-05-24 00:47:18
我杰森,我认为这段代码对你有帮助。
代码:
DataSet ds = new DataSet();
string query = "select * from tab_menu";
SqlConnection sqlConnection = new SqlConnection(@"Persist Security Info=False;User ID=sa;Password=sa;Initial Catalog=EasyAdmin;Data Source=.");
SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
sqlConnection.Open();
sqlDataAdapter.Fill(ds);
sqlConnection.Close();
dataGridView1.DataSource = ds.Tables[0];
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
dataGridView1.Columns.Add(btn);
btn.HeaderText = "Botoes";
btn.Text = "Clicar Aqui";
btn.Name = "btn";
btn.UseColumnTextForButtonValue = true;
并在DataGridView中创建事件(CellClick
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
{
//TODO - Button Clicked - Execute Code Here to change the text
}
}
发布于 2019-05-24 04:51:30
好的,为了实现这一点,我只是隐藏了保存照片字符串的列,并保留了添加按钮到rows..so的代码谢谢Silvio...appreciate it的帮助
private void frmRepoPremier_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
string query = "select * from Repos";
try
{
MySqlConnection sqlConnection = new MySqlConnection(MyConnectionString);
MySqlCommand sqlCommand = new MySqlCommand(query, sqlConnection);
MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(sqlCommand);
sqlConnection.Open();
sqlDataAdapter.Fill(ds);
sqlConnection.Close();
dgvBuildings.DataSource = ds.Tables[0];
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
dgvBuildings.Columns.Add(btn);
btn.HeaderText = "Photos";
btn.Text = "View";
btn.Name = "btn";
btn.UseColumnTextForButtonValue = true;
//HID THIS COLUMN TO REPLACE THE VIEW BUTTON
this.dgvBuildings.Columns[11].Visible = false;
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex, "Error: Load Data", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
if (connection.State == ConnectionState.Open)
{ connection.Close(); }
}
}
private void dgvBuildings_CellClick(object sender, DataGridViewCellEventArgs e)
{ var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
{
//OPEN IMAGE FROM BUTTON
System.Diagnostics.Process.Start(textBox1.Text);
}
}
private void dgvBuildings_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dgvBuildings.Rows[e.RowIndex];
textBox1.Text = row.Cells[12].Value.ToString();
try
{
if (row.Cells[12].Value != null)
{ pictureBox1.LoadAsync(row.Cells[12].Value.ToString()); }
else
{ return; }
}
catch (Exception)
{
pictureBox1.Image = null;
return;
}
}
}
https://stackoverflow.com/questions/56284207
复制相似问题