I'm using C# i want to make an application which requires me to have a datagridview of 10 columns and 8 rows.
However in design time i can only see one row.
I need all 8 rows visible in designtime as well as run time
So how can i programmatically add rows and make them visible.
I also will need to be able to randomly select values of the datagrid cells and put them into textboxes is this possible?
Thanks
Bobby Oakes
Loading

Satyapriya NayakRank #47 · 31.5K reputationPosted Jan 7, 2012, 8:49 PM EST
For 2nd part of your question
Try this..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Datagridview_bind
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oda;
DataSet ds;
string str;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bind();
}
void bind()
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from customer";
com = new OleDbCommand(str, con);
oda = new OleDbDataAdapter(com);
ds = new DataSet();
oda.Fill(ds, "customer");
dataGridView1.DataMember = "customer";
dataGridView1.DataSource = ds;
con.Close();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = "";
bool s1 = true;
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
if (!s1)
{
textBox1.Text += ", ";
}
textBox1.Text += cell.Value.ToString();
s1 = false;
}
}
}
}
Thanks
If this post helps you mark it as answer
Pravin GhadgeRank #1,340 · 1.3K reputationPosted Jan 7, 2012, 9:21 PM EST
1)on form_Load:Add rows programmatically.
private void form1_Load(object sender, EventArgs e)
{
datagridview.Rows.Add(8);
}
2)Use cell click event:display value of cell in textbox
private void datagridview_CellClick(object sender, DataGridViewCellEventArgs e)
{
textbox1.Text = datagridview.CurrentRow.Cells[0].Value.ToString();
textbox2.Text = datagridview.CurrentRow.Cells[1].Value.ToString();
}