dotolee

Hi there.

I have a datagridview that I populate via a table adapter and everything is working just fine. Now i'd like to give the user the option of:

selecting multiple records in the grid by checking a checkbox. this checkbox is available for each row of data and i created it by adding an unbound column.

What i'd like to do is enable a button on the same form only if one or more records have been selected by the user. how do i do this I was thinking about using the event:

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

End Sub

but this event can potentially be fired many times and for any column that's selected.

Any suggestions

Also, what's the easiest way to allow the user to edit the data for a given record I need to do another query when they request an edit because the current datagrid doesn't show all the details for the record. I intend to launch a new window to do this.

Please and thanks.



Re: Windows Forms General datagridview - testing values of an unbound column

dotolee

Maybe I should start off by asking a more general question about my approach all-together.

How would you recommend that I associate a label, if you will, with records that are displayed via a datagrid control The idea is that the user can select specific records, type in a grouping label which is just a text field, and then save this all to the database.

RIght now, i have a form with the datagrid to show the initial records, along with a checkbox next to each record. I have a Create Territory button that displays a new form. this form contains two main controls:

1. a text box that accept the territory label

2. a button to save the territory name.

But maybe i shouldn't be using a separate form at all. Maybe I should just do this on the initial form. Does it matter

Thanks.





Re: Windows Forms General datagridview - testing values of an unbound column

Zhi-Xin Ye - MSFT

1) . For the first question, you can handle the CellContentClick event along with the CurrentCellDirtyStateChanged event, since we want the CheckBox's state to be changed when we click them. Meanwhile, use a variable of int type to count the currently selected records, change the button's Enable property accordingly to this variable as the following code snippet illustrated.

Code Snippet

public partial class Form8 : Form
{
public Form8()
{
InitializeComponent();
}

private void Form8_Load(object sender, EventArgs e)
{
this.button1.Enabled = false;

DataTable dt = new DataTable();
dt.Columns.Add("id",typeof(int));
dt.Columns.Add("name");
for (int j = 0; j < 10; j++)
{
dt.Rows.Add(j, "row" + j.ToString());
}
this.dataGridView1.DataSource = dt;

this.dataGridView1.VirtualMode = true;

DataGridViewCheckBoxColumn ckc = new DataGridViewCheckBoxColumn();
ckc.Name = "CheckBoxColumn";
ckc.HeaderText = "Select";
this.dataGridView1.Columns.Add(ckc);

this.dataGridView1.CellContentClick +=
new DataGridViewCellEventHandler(dataGridView1_CellContentClick);
this.dataGridView1.CurrentCellDirtyStateChanged +=
      new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);
}

void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.dataGridView1.CurrentCell.ColumnIndex ==
this.dataGridView1.Columns["CheckBoxColumn"].Index)
{
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}

private int selectedCount = 0;

void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == this.dataGridView1.Columns["CheckBoxColumn"].Index)
{
object obj = this.dataGridView1[e.ColumnIndex,e.RowIndex].Value;
if (obj != null)
{
if ((bool)obj)
{
this.selectedCount++;
}
else
{
this.selectedCount--;
}
}

this.button1.Enabled = this.selectedCount > 0 true : false;
}
}
}


2). For the second, I recommend you adding a few Labels and TextBoxes to the same Form which the DataGridView is set up on, each attaching to a field in the data table, and bind the Text property of each TextBox to the DataSource which is the same as the DataSource of the DataGridView, thus while selecting different records on the DataGridView, the content in the TextBoxes will be changed accordingly ,something like this:

this.TextBox1.DataBindings.Add("Text",ds.Table[0],"id");
//´.etc....
this.DataGridView.DataSource = ds.Table[0];