i have the following situation. how can I change the value in the column in a dataGridView?
thanks!

Recommended Answers

All 6 Replies

Change the value in the dataset it is bound too, and then update the datagrid.

you can also change the value by just double clicking in it . but this will not change value in your dataset or datatable

Regards

If the DataGridView is databound, you shouldn't directly modify the content of the cell. Instead, you should modify the databound object. You can access that object through the DataBoundItem of the DataGridViewRow :

MyObject obj = (MyObject)dataGridView.CurrentRow.DataBoundItem;
obj.MyProperty = newValue;

For more information regarding it read it. http://dapfor.com/en/net-suite/net-grid/tutorial

Thank you all.Here is the solution, which I found myself.There are 2 forms - parent and child. the first part of the code is for parent form.It takes data for dataGridView from Form2,which is child

//in parent form:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.MdiParent = this;
            frm.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = Form2.textOnForm2;
            dataGridView1[2,0].Value= Form2.textOnForm2;
//in child form:
 public partial class Form2 : Form
    {
        public static string textOnForm2;
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2.textOnForm2 = textBox1.Text;
            Form1 frm1 = new Form1();
            frm1.ShowDialog();
            frm1.Close();
        }
    }

if your prob is solved then please mark this thread as solved.

Regards

Hello, you can use below code:

private void DataGridView1_CellValueChanged(
    object sender, DataGridViewCellEventArgs e)
{
    // Update the balance column whenever the value of any cell changes.
    UpdateBalance();
}

private void DataGridView1_RowsRemoved(
    object sender, DataGridViewRowsRemovedEventArgs e)
{
    // Update the balance column whenever rows are deleted.
    UpdateBalance();
}

private void UpdateBalance()
{
    int counter;
    int balance;
    int deposit;
    int withdrawal;

    // Iterate through the rows, skipping the Starting Balance row. 
    for (counter = 1; counter < (DataGridView1.Rows.Count - 1);
        counter++)
    {
        deposit = 0;
        withdrawal = 0;
        balance = int.Parse(DataGridView1.Rows[counter - 1]
            .Cells["Balance"].Value.ToString());

        if (DataGridView1.Rows[counter].Cells["Deposits"].Value != null)
        {
            // Verify that the cell value is not an empty string. 
            if (DataGridView1.Rows[counter]
                .Cells["Deposits"].Value.ToString().Length != 0)
            {
                deposit = int.Parse(DataGridView1.Rows[counter]
                    .Cells["Deposits"].Value.ToString());
            }
        }

        if (DataGridView1.Rows[counter].Cells["Withdrawals"].Value != null)
        {
            if (DataGridView1.Rows[counter]
                .Cells["Withdrawals"].Value.ToString().Length != 0)
            {
                withdrawal = int.Parse(DataGridView1.Rows[counter]
                    .Cells["Withdrawals"].Value.ToString());
            }
        }
        DataGridView1.Rows[counter].Cells["Balance"].Value =
            (balance + deposit + withdrawal).ToString();
    }
}

See detail at http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.