|
Dear All,
I ama newbie to Windows development. I am currently in a process of creating a windows application using C# and VS 2008.
My Question,
I have designed FORM1 with a datagridview dgvForm1 and am populating data into it using LINQ queries. The data is populating fine into the dgvForm1.
I have a checkbox column in the dgvForm1 to select the checked rows.
I have a button in FORM1 called DISPLAY.
I have FORM2 with datagridview dgvForm2.
All I want to do is ...allow the user to select the required rows from dgvForm1 using checkboxs. After selecting, the user clicks on the DISPLAY which willshow FORM2 . I want to display the selected rows from dgvForm1 in the datagridview(dgvForm2) ofFORM2.
Please suggest solutions. Thanks,
|
| spankyleo Saturday, July 25, 2009 11:59 PM |
Hi spankyleo,
Thanks for the reply. Based on your code and answers, you directly bind the data to dataGridView1. I have another method:
1. Initial the data source of dataGridView1 to a BindingSource:
a. Create a BindingSource.
b. Set its data source to DAL.GetALL().
c. Set the data source of dataGridView1 to this BindingSource.
2. To get the data source of the DataGridView in Form2, we can also create a BindingSource and add checked data to it.
a. Create a new BindingSource.
b. Check the check state of each row in dataGridView1 add and the data underlying to the new BindingSource.
c. Transfer this BindingSource to Form2 and bind it to the DataGridView.
This is the code snippet: private BindingSource _bindSrc1;
//Initial the data source of the dataGridView1
private void InitialDataSource()
{
_bindSrc1 = new BindingSource();
_bindSrc1.DataSource = DAL.GetAll();
this.dataGridView1.DataSource = _bindSrc1;
}
//Get the new data source for the DataGridView in Form2.
private BindingSource GetCheckedDataSource()
{
BindingSource newBindSrc = new BindingSource();
//Traverse all the rows and related data to newBindSrc
//if the check box in this row is checked.
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
DataGridViewRow row = this.dataGridView1.Rows[i];
if (row.IsNewRow == false)
{
//Add data to newBindSrc if checked.
if (Convert.ToBoolean(row.Cells[SelectColumnIndex].Value))
newBindSrc.Add(_bindSrc1.List[i]);
}
}
return _bindSrc1;
}
Let me know if this helps. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byAland LiMSFT, ModeratorFriday, July 31, 2009 12:37 PM
-
|
| Aland Li Thursday, July 30, 2009 5:26 AM |
Im a newbie too... i did not check it but how about something like this
Dim drCollection As DataRowCollection
For Each x As DataRow In DataGridView1.Rows
If x.Item("CheckedColumn").ToString = "Checked" Then
drCollection.Add(x)
End If
Next
Form2.DataGridView2.DataSource = drCollection
|
| Ranhiru123 Sunday, July 26, 2009 6:25 AM |
Am using C# with VS 2008 , this my code for reading the checked value of rows,,,, I have defined a constructor in Form2 to take in string values. Then in form2 initialization am assigning the string value to datagridview2 in form 2.. Am not sure if its correct to do so ..... but am not getting the required result.i.e my datagrid2 is not getting populate with the checked rows from datagrid1. please suggest solutions,,,thanks
string
data = string.Empty; //data = this foreach (DataGridViewRow row in dataGridView1.Rows { if (row.Cells[SelectColumnIndex].Value != null && Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true) { foreach (DataGridViewCell cell in row.Cells) { if (cell.OwningColumn.Index != SelectColumnIndex) { data += (cell.Value + ","); } } data += "\n"; } } Form2 fm = new Form2(data); fm.Show();
}
public Form2(String data)
{
InitializeComponent();
this.dgv1Fm2.DataSource = data;
}
|
| spankyleo Sunday, July 26, 2009 11:49 AM |
Hi spankyleo,
Before we go any further, would you mind answering these questions?
1. What kind of data source did you bind to the DataGridView? DataTable, List or something else?
2. Is the checkbox column bound? In other words, how did you initialize the check states of the checkbox column?
From your description, your code snippet cannot work. It is because string cannot be used as a data source. The data source which can be bound to a DataGridView must implement IList interface, such as DataTable, List, Array and so on. I suggest you filter the data source of the DataGridView in form1 and then transfer the filtered data source to form2. Then you can bind that filtered data source to the DataGridView in form2.
Please feel free to tell me if you need a code snippet or more detail suggestions.
Best regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Edited byAland LiMSFT, ModeratorTuesday, July 28, 2009 8:13 AM
-
|
| Aland Li Tuesday, July 28, 2009 2:30 AM |
A code snippet would be really usefull, ..... am trying to get the data displayed since days.....with no luck.
Cheers |
| spankyleo Tuesday, July 28, 2009 9:56 PM |
Hi spankyleo,
Could you please answer my questions at first? I cannot write code without answers to those questions.
Best regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Wednesday, July 29, 2009 2:17 AM |
1. What kind of data source did you bind to the DataGridView? DataTable, List or something else? I am using LINQ2SQL DBML file to link the MSSQL database. The queries are based on linq , my DATA ACCESS LAYER(Custom class) consists of a method containing a LINQ query. This method is assigned to the datagrid. eg datagridview1.Datasource = DAL.GetALL();
2. Is the checkbox column bound? In other words, how did you initialize the check states of the checkbox column? I used the ADD COLUMN property by right clicking the datagridvew and selected the checkboxcolumn.
Thanks,
|
| spankyleo Wednesday, July 29, 2009 6:52 PM |
Hi spankyleo,
Thanks for the reply. Based on your code and answers, you directly bind the data to dataGridView1. I have another method:
1. Initial the data source of dataGridView1 to a BindingSource:
a. Create a BindingSource.
b. Set its data source to DAL.GetALL().
c. Set the data source of dataGridView1 to this BindingSource.
2. To get the data source of the DataGridView in Form2, we can also create a BindingSource and add checked data to it.
a. Create a new BindingSource.
b. Check the check state of each row in dataGridView1 add and the data underlying to the new BindingSource.
c. Transfer this BindingSource to Form2 and bind it to the DataGridView.
This is the code snippet: private BindingSource _bindSrc1;
//Initial the data source of the dataGridView1
private void InitialDataSource()
{
_bindSrc1 = new BindingSource();
_bindSrc1.DataSource = DAL.GetAll();
this.dataGridView1.DataSource = _bindSrc1;
}
//Get the new data source for the DataGridView in Form2.
private BindingSource GetCheckedDataSource()
{
BindingSource newBindSrc = new BindingSource();
//Traverse all the rows and related data to newBindSrc
//if the check box in this row is checked.
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
DataGridViewRow row = this.dataGridView1.Rows[i];
if (row.IsNewRow == false)
{
//Add data to newBindSrc if checked.
if (Convert.ToBoolean(row.Cells[SelectColumnIndex].Value))
newBindSrc.Add(_bindSrc1.List[i]);
}
}
return _bindSrc1;
}
Let me know if this helps. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byAland LiMSFT, ModeratorFriday, July 31, 2009 12:37 PM
-
|
| Aland Li Thursday, July 30, 2009 5:26 AM |
Hi Aland,
Thanks for your help. Do i have to initialize a constrouctor in form2 class ? (eg. Bindingsource). I basically want form2 to show the select rows from FORM1, on a button click event. |
| spankyleo Thursday, July 30, 2009 7:36 PM |
Hi spankyleo,
Yes, we need to transfer the data source returned by GetCheckedDataSource to Form2. Pass it as an argument of the constructor of Form2 is one way, we can also define a public method to bind data source to the DataGridView in Form2. This is a code snippet: public class Form1 : Form
{
private void ShowForm2()
{
Form2 form = new Form2();
form.BindData(this.GetCheckedDataSource());
}
}
public class Form2 : Form
{
public void BindData(BindingSource bindSrc)
{
this.dataGridView1.DataSource = bindSrc;
}
}
Let me know if this helps. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byAland LiMSFT, ModeratorFriday, July 31, 2009 12:37 PM
- Unmarked As Answer byspankyleo Sunday, August 02, 2009 11:54 AM
-
|
| Aland Li Friday, July 31, 2009 1:58 AM |
public class Form2 : Form
{
public void BindData(BindingSource bindSrc)
{
this.dataGridView1.DataSource = bindSrc;
}
}
The dataGridView1 here is it referring to datagridview of Form1 or form2 ,,,, am getting an error
"Error1'WinApp.Form1.Form2' does not contain a definition for 'dataGridView1' and no extension method 'dataGridView1' accepting a first argument of type 'WinApp.Form1.Form2' could be found (are you missing a using directive or an assembly reference?)" |
| spankyleo Sunday, August 02, 2009 11:58 AM |
Hi spankyleo,
this.dataGridView1 refers to the DataGridView in form2. You need to change this to the name of that DataGridView. For example, if it names dgv1 in your program, the code can be: this.dgv1.DataSource = bindSrc;
Best regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Monday, August 03, 2009 2:36 AM |
Hi, Thanks for that. When I compile the code am getting a nullreference exception at this point
if
(Convert.ToBoolean(row.Cells[SelectColumnIndex].Value))
newBindSrc.Add(_bindSrc1.List[i]); <---- here "Object reference not set to an instance of an object" "Use the new keyword to create an object instance"
|
| spankyleo Monday, August 03, 2009 8:55 AM |
Hi spankyleo,
At first, you need to check if the SelectColumnIndex refers to a legal column index.
Second, you need to check if row.Cells[SelectColumnIndex].Value is null. You can follow the code below to set the null value of the column: DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
col.DefaultCellStyle.NullValue = false;
Let me know if this helps. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Tuesday, August 04, 2009 2:22 AM |
Hello Thanks , the SelectColumnIndex is an integer initialized before the actual method
static int SelectColumnIndex = 0;
If you refer to my post above (Sunday, July 26, 2009 11:49 AM) , that code I have defined in the button click event, and just b4 the method starts i have initialized an integer (SelectColumnIndex).
|
| spankyleo Tuesday, August 04, 2009 5:07 PM |
Hi spankyleo,
Do you mean after initializing SelectColumnIndex correctly, you still have the nullreference exception?
Aland Li Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Wednesday, August 05, 2009 1:49 AM |
yeah I have initialized it and still am getting the nullreferrence exception. |
| spankyleo Wednesday, August 05, 2009 3:58 PM |
Hi spankyleo,
Could you please provide a code snippet you wrote recently so that I can figure out the root cause of the issue?
Best regards, Aland Li Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Thursday, August 06, 2009 2:50 AM |
//IN FORM 1
private BindingSource _bindSrc1;
//Initial the data source of the dataGridView1
private void InitialDataSource()
{
_bindSrc1 = new BindingSource();
_bindSrc1.DataSource = DAL.GetAllExcelOrders();
this.dataGridView1.DataSource = _bindSrc1;
}
//static int SelectColumnIndex = 0;
//Get the new data source for the DataGridView in Form2.
private BindingSource GetCheckedDataSource()
{
BindingSource newBindSrc = new BindingSource();
//Traverse all the rows and related data to newBindSrc
//if the check box in this row is checked.
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
DataGridViewRow row = this.dataGridView1.Rows[i];
if (row.IsNewRow == false)
{
//Add data to newBindSrc if checked.
//DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
//col.DefaultCellStyle.NullValue = false;
if (Convert.ToBoolean(row.Cells[i].Value))
{
newBindSrc.Add(_bindSrc1.List[i]);
}
}
}
return _bindSrc1;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2(this._bindSrc1);
form.Show();
}
////IN FORM 2
private BindingSource bd;
public Form2(BindingSource bd)
{
InitializeComponent();
this.bd = bd;
this.dataGridView1.DataSource = bd;
}
|
| spankyleo Thursday, August 06, 2009 6:43 PM |
Hi spankyleo,
You need to change this code: if (Convert.ToBoolean(row.Cells[i].Value))
{
newBindSrc.Add(_bindSrc1.List[i]);
}
into this: if(row.Cells[i].Value != null && Convert.ToBoolean(row.Cells[i].Value))
{
newBindSrc.Add(_bindSrc1.List[i]);
}
This would check the value and ignore null values.
If you want to do this:
col.DefaultCellStyle.NullValue = false;
Please write the code before you bind data to the DataGridView.
Best regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Friday, August 07, 2009 3:05 AM |