Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView data editting
 

DataGridView data editting

Hello again..
now i'm having problem trying to edit data in my datagridview, here's what i have..

in my datagridview, there are textboxcolumns, comboboxcolumn, and buttoncolumn, here's the sequence of what i'm trying to achieve
1. the user would select the appropriate item in the combobox column, then push the button in the button column.. (done)
2. the appropriate dialog would pop up based on the item selected in the combobox (done)
3. the user would make necessary change in that dialog box and the value would be say for example "xyz" (done)
4. when the user push "OK" button in the dialog box, the data in the textboxcolumn in the row that was being pushed need to change to "xyz" instead of the default value.. (this is the part that i'm having trouble with)

I've tried using method, however, when i execute and try the last part (#4) the error will pop up and say that the index is out of range, even though it is not at all..

please help..

thank you in advance..
somepenguin  Thursday, July 23, 2009 6:43 AM

Hi somepenguin,

From my experience, we need to call ShowDialog method to pop up a edit form. This the detail reason:

· If we call ShowDialog method, it would return only when the DialogResult property of the form is set or the form is closed. It allows us to do some operations on the form, such as check a check box, then set the DialogResult property to return to the main method and executes the code after the ShowDialog calling. In other words, the main code where we call the ShowDialog method and the code executed in the shown form is in a same UI thread.

· If we call Show method, it would return just after the edit form is shown. It would not wait for us to do some UI operations. For example, if we check a check box on the edit form, it would affect the Area property of the edit form, but it would not affect the Area value we fetched when the Show method returned. This is the code snippet, in which I point out the key issue:

private void GetFootingData(int w, int l, int row, int column)

{

frmFooting ftg = new frmFooting(w, l);

//This would return just after the form is shown.

ftg.Show();

//The value we get here is calculated just after the form is shows.

//The value is indeed calculated only in constructor of frmFooting.

dgvTakeOff.Rows[row].Cells[column].Value = ftg.Area;

}

In other words, the code executed in GetFootingData function and the code executed in frmFooting are not in the same thread.

This is the modified code snippet in which we call ShowDialog method to pop up the edit Form:
Get data method:

private void GetFootingData(int w, int l, int row, int column)

{

frmFooting ftg = new frmFooting(w, l);

//Please call ShowDialog here. This method can return only when you set the DialogResult of the form.

//If you call Show method. It would return rapidly. So whther the checkbox is checked cannot affect the result.

if (ftg.ShowDialog() == DialogResult.OK)

{

dgvTakeOff.Rows[row].Cells[column].Value = ftg.Area;

}

}

The frmFooting form:

public partial class frmFooting : Form

{

int width = 0;

int length = 0;

int area = 0;

public frmFooting(int width, int length)

{

InitializeComponent();

this.width = width;

this.length = length;

}

public int Area

{

get { return area; }

set { area = value; }

}

private void Calculation()

{

int top, bottom;

if (chkTop.Checked == true)

{

top = width * length;

}

else top = 0;

if (chkBottom.Checked == true)

{

bottom = width * length;

}

else bottom = 0;

area = top + bottom;

label1.Text = area.ToString();

}

private void btnOK_Click(object sender, EventArgs e)

{

this.DialogResult = DialogResult.OK;

}

private void btnCancel_Click(object sender, EventArgs e)

{

this.DialogResult = DialogResult.Cancel;

}

private void chkTop_CheckedChanged(object sender, EventArgs e)

{

Calculation();

}

private void chkBottom_CheckedChanged(object sender, EventArgs e)

{

Calculation();

}

}

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  Monday, July 27, 2009 6:35 AM
Here's the code that i've written so far..

This is when the button is click..
Code:
//code when the button in DataGridView is click, it would get the data from ComboBoxColumn
private void dgvTakeOff_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == _Button.Index)
{
if (dgvTakeOff.Rows[e.RowIndex].Cells[_Properties.Index].Value == null)
{
MessageBox.Show("Whatever", "Whatever", MessageBoxButtons.OK);
}
else
{
string comboBoxText = dgvTakeOff.Rows[e.RowIndex].Cells[_Properties.Index].Value.ToString();

if (comboBoxText == "Footing")
{
rowIndex = e.RowIndex;
columnIndex = _Quantity.Index;
frmFooting ftg = new frmFooting();
ftg.Data(2, 10);
ftg.Show();
}
if (comboBoxText == "Wall")
{
MessageBox.Show("Here comes the Wall", "Wall", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
This is the method after the "OK" button in the dialog box is clicked

Code:
       private void btnOK_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
f.GetData(area);

this.Close();
}
And this is the GetData method in the Form1.cs

Code:
        public void GetData(int num)
{
quantity = num;
dgvTakeOff.Rows[rowIndex].Cells[columnIndex].Value = quantity.ToString();
//this is where the error would come up as the index is being ArgumentOutOfRange
}
somepenguin is online now Add to somepenguin's Reputation Report Post Edit/Delete Message
somepenguin  Thursday, July 23, 2009 9:22 PM
Hi again..
I've been trying different ways and method to counter the problem above, however, it hasn't been successful.. here's what my problem is like right now.. the sending data part from datagridview to another form is working fine, however, when i want to retrieve the data back, i still can't do anything about it.. this is what i've gotten so far..

This is the code when i want to send the data out to the frmFooting.cs (it's a method)
Code:
        private void GetFootingData(int w, int l, int row, int column)
{
frmFooting ftg = new frmFooting(w,l);
ftg.Show();

dgvTakeOff.Rows[row].Cells[column].Value = ftg.Area;
}
And here's the code from the frmFooting.cs
Code:
    public partial class frmFooting : Form
{
int width = 0;
int length = 0;
int area = 0;

public frmFooting(int width, int length)
{
InitializeComponent();
this.area = Calculation(width, length); // if i changed this to something like "15", then when i pushed "OK" on the DataGridView, the data in the cell i wanted it to change actually change to "15" but it's not what i really wanted, i wanted it to change to "15" when i push "OK" in this frmFooting form..
this.width = width;
this.length = length;
}

public int Area
{
get { return area; }
set { area = value; }
}

private int Calculation(int w, int l)
{
int top, bottom;

if (chkTop.Checked == true)
{
top = w * l;
}
else top = 0;

if (chkBottom.Checked == true)
{
bottom = w * l;
}
else bottom = 0;

area = top + bottom;
label1.Text = area.ToString();

return area;
}

private void btnOK_Click(object sender, EventArgs e)
{
this.Close();
}

private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}

private void chkTop_CheckedChanged(object sender, EventArgs e)
{
Calculation(width, length);
}

private void chkBottom_CheckedChanged(object sender, EventArgs e)
{
Calculation(width, length);
}
}
somepenguin  Saturday, July 25, 2009 6:19 AM

Hi somepenguin,

From my experience, we need to call ShowDialog method to pop up a edit form. This the detail reason:

· If we call ShowDialog method, it would return only when the DialogResult property of the form is set or the form is closed. It allows us to do some operations on the form, such as check a check box, then set the DialogResult property to return to the main method and executes the code after the ShowDialog calling. In other words, the main code where we call the ShowDialog method and the code executed in the shown form is in a same UI thread.

· If we call Show method, it would return just after the edit form is shown. It would not wait for us to do some UI operations. For example, if we check a check box on the edit form, it would affect the Area property of the edit form, but it would not affect the Area value we fetched when the Show method returned. This is the code snippet, in which I point out the key issue:

private void GetFootingData(int w, int l, int row, int column)

{

frmFooting ftg = new frmFooting(w, l);

//This would return just after the form is shown.

ftg.Show();

//The value we get here is calculated just after the form is shows.

//The value is indeed calculated only in constructor of frmFooting.

dgvTakeOff.Rows[row].Cells[column].Value = ftg.Area;

}

In other words, the code executed in GetFootingData function and the code executed in frmFooting are not in the same thread.

This is the modified code snippet in which we call ShowDialog method to pop up the edit Form:
Get data method:

private void GetFootingData(int w, int l, int row, int column)

{

frmFooting ftg = new frmFooting(w, l);

//Please call ShowDialog here. This method can return only when you set the DialogResult of the form.

//If you call Show method. It would return rapidly. So whther the checkbox is checked cannot affect the result.

if (ftg.ShowDialog() == DialogResult.OK)

{

dgvTakeOff.Rows[row].Cells[column].Value = ftg.Area;

}

}

The frmFooting form:

public partial class frmFooting : Form

{

int width = 0;

int length = 0;

int area = 0;

public frmFooting(int width, int length)

{

InitializeComponent();

this.width = width;

this.length = length;

}

public int Area

{

get { return area; }

set { area = value; }

}

private void Calculation()

{

int top, bottom;

if (chkTop.Checked == true)

{

top = width * length;

}

else top = 0;

if (chkBottom.Checked == true)

{

bottom = width * length;

}

else bottom = 0;

area = top + bottom;

label1.Text = area.ToString();

}

private void btnOK_Click(object sender, EventArgs e)

{

this.DialogResult = DialogResult.OK;

}

private void btnCancel_Click(object sender, EventArgs e)

{

this.DialogResult = DialogResult.Cancel;

}

private void chkTop_CheckedChanged(object sender, EventArgs e)

{

Calculation();

}

private void chkBottom_CheckedChanged(object sender, EventArgs e)

{

Calculation();

}

}

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  Monday, July 27, 2009 6:35 AM

You can use google to search for other answers

Custom Search

More Threads

• Datagridview numeric property validation
• Dataview RowFilters
• Problem in Add Connection dialog box
• what is equivalent way of loading hierarchical data in datagridview using dataset (like .Net 1.1 Datagrid)
• DataGridView automatically selects first row after 60 seconds of inactivity
• Multiple DataGridViews Same DataSource Different Filters
• ListView AllowColumnReorder
• Two combobox one table question
• Change the Look and Feel of Datagridview
• Generic BindingList copy constructor bug