Amit Bisht
Amit Bisht

Reputation: 5136

Passing DataGridViewRow as referance

I want to pass DataGridviewRow as a ref where i am assigning some value to its cells but i am unable to do this

foreach(DataGridViewRow dgr in dgvMarksEntryByClassWise.Rows)
{
   RowValueSet(ref dgr);
}  

Here it is giving Compile time Error because dgr is an foreach iteration variable
Also i tried to do it with for loop

for (int i = 0; i < dgvMarksEntryByClassWise.Rows.Count; i++)
{
  RowValueSet(ref dgvMarksEntryByClassWise.Rows[i]);
}  

But here also it is giving Compile time error:
A property,indexer or dynamic member acces cannot passed as an out reference

I referd this question asked on above errors but did't Found any appropriate solution of my problem

Please Suggest me how to Do this

Update Code

void RowValueSet(ref DataGridViewRow dgr)
{
dgr.Cells["StudentZero"].Value = ss.Where(w => w.MarksheetMarks == "0").Count();
if (ss.Count() != 0)
dgr.Cells["StudentISEmpty"].Value = Convert.ToInt16(lblTotlatStudent1.Text) - ss.Count();
else
dgr.Cells["StudentISEmpty"].Value = 0;
dgr.Cells["StudentEntry"].Value = ss.Count();
}

Upvotes: 3

Views: 1523

Answers (4)

Adam Valpied
Adam Valpied

Reputation: 178

I agree with the comments about not passing the row using ref. It is a reference type. You generally only use that keyword with value types (such as int, etc).

I've had problems with the DataGridView not updating after changing cell values and often have needed to call dataGridView1.Invalidate(); It depends how things are hooked up. If you use data-binding with a class that inherits from INotifyPropertyChanged then it always seems to work, but if you just change cell values I have noticed the odd time that it would not update.

Here is an example that works. Create a new app, add an empty DataGridView onto the form and replace the code with the following. This proves that you do not need to use ref.

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "Column1" });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "Column2" });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "Column3" });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "Column4" });

            for (int i = 0; i <= 4; i++)
            {
                dataGridView1.Rows.Add();
            }

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                RowValueSet(row);
            }
        }

        private void RowValueSet(DataGridViewRow row)
        {
            row.Cells["Column1"].Value = "1";
            row.Cells["Column2"].Value = "2";
            row.Cells["Column3"].Value = "3";
            row.Cells["Column4"].Value = "4";
        }
    }

I imagine that your problem lies elsewhere

Upvotes: 0

batwad
batwad

Reputation: 3665

As stated in the comments above, you do not need to pass DataGridViewRow by reference. Remove ref from the parameter.

void RowValueSet(DataGridViewRow dgr)
{
    dgr.Cells["StudentZero"].Value = ss.Where(w => w.MarksheetMarks == "0").Count();
    if (ss.Count() != 0)
        dgr.Cells["StudentISEmpty"].Value = Convert.ToInt16(lblTotlatStudent1.Text) - ss.Count();
    else
        dgr.Cells["StudentISEmpty"].Value = 0;
    dgr.Cells["StudentEntry"].Value = ss.Count();
}

If the values in the grid do not appear to be updating, it's for another reason. Do you re-bind the grid when you need to refresh the display?

Upvotes: 2

Sebastien H.
Sebastien H.

Reputation: 7176

you cannot assign an iterated, enumerated item using a "foreach" or even a "for". i got around that once, using that piece of code :

List<DataGridViewRow> lstDgr = new List<DataGridViewRow>();
foreach(DataGridViewRow dgr in dgvMarksEntryByClassWise.Rows)
{
   DataGridViewRow dgrTemp = dgr;
   RowValueSet(ref dgrTemp);
   lstDgr.Add(dgrTemp);
}  
dgvMarksEntryByClassWise.Rows.Clear();
dgvMarksEntryByClassWise.Rows.AddRange(lstDgr); //Not sure about the AddRange, try the Add method instead

Hope this helps

Upvotes: 2

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101701

How about this ?

for (int i = 0; i < dgvMarksEntryByClassWise.Rows.Count; i++)
{
   var myRow = dgvMarksEntryByClassWise.Rows[i];
   RowValueSet(ref myRow);
} 

Upvotes: 1

Related Questions