Hi,
I want to add multiple gridviewbutton column to datagrid. For ex: If i have 20 records, i want to add 10 button columns each row. In this case , 2 row with 10 column each.
when i am trying to implement this. all the columns are displayed in each row(i.e. 20 button column each row)
Please help!!
Loading
Vijay YadavPosted Feb 9, 2011, 4:14 AM
Your Welcome..
And Good to hear your that query got resolved and thanks for posting the answer here.
jaya joshiPosted Feb 9, 2011, 4:04 AM
Sorry for late reply..My Problem get solved..
to get the value of the button cell clicked,i write this code:
DataGridViewButtonCell cell = (DataGridViewButtonCell)
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
string cellV = cell.Value.ToString();
Thanks..
Vijay YadavPosted Jan 21, 2011, 12:13 AM
Have your thread got solved or not?
Thanks,
Vijay
Vijay YadavPosted Jan 19, 2011, 5:55 AM
Your Welcome!!!
For which button got clicked, you can write the following code on CellMouseClick event of datagridview
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("write your column name here"))
{
MessageBox.Show(dataGridView1.Columns[e.ColumnIndex].Name.ToString());
}
Thanks,
Vijay
jaya joshiPosted Jan 19, 2011, 5:34 AM
Can you please tell how to implement click event for the buttoncell clicked. How do we know which button is clicked?
Thanks,
Jaya
Vijay YadavPosted Jan 19, 2011, 1:40 AM
The code to create two rows with 10 button column each dynamically is
dataGridView1.ColumnCount = 10; // Create 10 button columns
for (int i = 0; i < 2; i++)
{
DataGridViewRow row = new DataGridViewRow();
for (int j = 0; j < 10; j++)
{
row.Cells.Add(new DataGridViewButtonCell());
}
dataGridView1.Rows.Add(row); // this is used to add new row with 10 buttons.
}
I hope you will get the idea.
Thanks
Vijay
jaya joshiPosted Jan 19, 2011, 1:05 AM
In the given code, i am creating 6 buttons instead of 10 buttons.
You just provide me the code to create two rows with 10 button column each dynamically.
Thanks,
Jaya
Vijay YadavPosted Jan 19, 2011, 12:24 AM
What i understood is you want 10 buttons in each row (i.e. in 2 rows) rite but it depends upon the xml file?
i haven't work with xml.
but i have done somthing like this with the SQL database.
Achcha tell me the code you have provided here is showing 6 buttons each row?
Looking forward for your reply
Thanks
Vijay
jaya joshiPosted Jan 18, 2011, 11:03 PM
I am getting the no. of records from xml.
The c ode you have provided is to create 10 buttons. how to add the next 10 columns in next row.
like for 20 records we need to have 2 rows with 10 button column each.
I Hope now u understand my issue.
Vijay YadavPosted Jan 18, 2011, 5:49 AM
This is according to your first post.
You want to add 10 buttons to datagridview dynamically you can add like this
for (int i = 0; i < 10; i++)
{
DataGridViewButtonColumn btnView = new DataGridViewButtonColumn();
btnView.HeaderText = "Button" + i;
btnView.Name = "Button" + i;
btnView.Text = "Button" + i;
btnView.Width = 50;
btnView.UseColumnTextForButtonValue = true;
dgvCustomer.Columns.Add(btnView);
}
As per your second, you reading from xml file then also you are dividing by six then what the use of that file anyway you can add 10 buttons to each row like this..
Thanks,
Vijay
jaya joshiPosted Jan 18, 2011, 5:31 AM
My Code is as follows for 6 column each row:
Code is here
private void toolStripButton7_Click(object sender, EventArgs e)
{
initializeDatagrid();
// dataGridView1.Show();
DataSet ds = new DataSet();
dataGridView1.Show();
ds.ReadXml("D:\\TestPdf\\Editor\\latest epuub editor\\Editor1\\Editor\\bin\\Debug" + "\\specialCh.xml");
//DataGridViewButtonColumn bc = new DataGridViewButtonColumn[dt.Rows.Count];
DataTable dt = ds.Tables["SpecialCharacter"];
int count = dt.Rows.Count;
double rw = (double)dt.Rows.Count / 6;
double rwcount = System.Math.Ceiling(rw);
int l = 0;
DataGridViewButtonColumn[] bc;//= new DataGridViewButtonColumn[6];
DataGridViewRow[] rwd = new DataGridViewRow[3];
for (int i = 0; i < rwcount; i++)
{
// dataGridView1.Rows.Add(rwd[i]);
bc = new DataGridViewButtonColumn[6];
for (int j = 0; j < 6; j++)
{
if (l > count-1)
{
break;
}
if (l <= count-1)
{
bc[j] = new DataGridViewButtonColumn();
bc[j].Text = dt.Rows[l]["Character"].ToString();
bc[j].Width = 20;
bc[j].UseColumnTextForButtonValue = true;
dataGridView1.Columns.Add(bc[j]);
}
l++;
}
dataGridView1.Rows.Add(bc);
}
}
Vijay YadavPosted Jan 18, 2011, 5:24 AM
I got your question, i want to know you are adding 10 button columns dynamically or through design. Can you tell or write your code or give some hint so that i can able to answer it.
Thanks
vijay