Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i mean say is

i have

1) Datagridview
2) 4 columns in datagridview...ie id,name,subject,marks
3) label under datagridview
4) i want to get sum of selected row of marks column...ie if want to get sum of 2 rows then i have to get sum of 2 subjects only but not total marks column sum

i have tried but i am not getting for dragged row only

What I have tried:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int sum = 0;
int i = e.RowIndex;
DataGridViewRow selectedrow = dataGridView1.Rows[i];
sum += Convert.ToInt32(selectedrow.Cells[6].Value);
label1.Text = sum.ToString();
}






Also tried this

private void autosum()
{
int sum = 0;
for(int i = 0;i<dataGridView1.Rows.Count;++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[6].Value);
}

label1.Text = "Total sum is :" + sum.ToString();
}
Posted
Updated 22-May-18 2:39am
Comments
Richard MacCutchan 18-May-18 15:20pm    
What is the problem?
Prateek gsharma 22-May-18 6:41am    
ere i am getting total sum marks column using above code...but requirement is if i select 1 cell then it should display that value only...if i select 2 cells of marks column then it should be displayed sum of 2 cells only... & so on....
Prateek gsharma 19-May-18 1:52am    
here i am getting total sum marks column using above code...but requirement is if i select 1 cell then it should display that value only...if i select 2 cells of marks column then it should be displayed sum of 2 cells only... & so on....

Prateek gsharma 21-May-18 10:00am    
can any one help me?

1 solution

Quote:
i want to get sum of selected row of marks column

If i understand you correctly, you want to get collection of DataGridView.SelectedRows[^] then to display sum(marks) in label.
So, you should use DataGridView.SelectionChanged Event (System.Windows.Forms)[^] to be able to achieve that. Use below code inside that event (body of event):
C#
int sum = 0;
foreach(DataGridViewRow dgr in dataGridView1.SelectedRows)
{
    sum+= dgr[6].Value;
}
label1.Text = sum.ToString();


Another way is to use Linq[^]:
C#
label1.Text = dataGridView1.SelectedRows.Cast<DataGridViewRow>().Sum(dgr=>dgr.Cells[6].Value).ToString();
 
Share this answer
 
v2
Comments
Prateek gsharma 24-May-18 13:44pm    
i am getting error like "cannot apply indexing with[] to an expression of type "datagridview"....

can i provide you attachment to see my exact requirment?how attach word docs to this page?
Maciej Los 24-May-18 15:52pm    
So, try to replace:
Sum(dgr=>dgr[6].Value)

with
Sum(dgr=>dgr.Cells[6].Value)

See: How to: Manipulate Rows in the Windows Forms DataGridView Control | Microsoft Docs[^]
Prateek gsharma 25-May-18 9:41am    
Sum(dgr=>dgr.Cells[6].Value)

still not working
Prateek gsharma 25-May-18 9:43am    
sum(dgr=>dgr.Cells[6].Value);

now i am getting "method name expected" error
Maciej Los 25-May-18 11:21am    
Well, i was lazy to re-construct your issue. I'll promise to take a look closer and let you know about progress.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900