Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 23rd, 2007, 01:30 PM
Authorized User
 
Join Date: Feb 2007
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default How add data into DataGrid dynamically

Hi All,

I am trying to add data into grid but it doesn't work me. Please can anyone take a look my code and tell me what am doing wrong.

Thanks,


//This data grid has two columns and initially has one row

int countVisitorVarRows = 0;

someforLoop
{
if (newVisitorVarRow == true)
{
   countVisitorVarRows++;
   dataGridView1.Rows.Add(); //adding second row
   dataGridView1.Rows[countVisitorVarRows].Cells[0].Value = str4[i]; //get error here outBound index
   dataGridView1.Rows[countVisitorVarRows].Cells[1].Value = str3[i]; //get error here outBound index


}

if (newVisitorVarRow == false)
{
   dataGridView1.Rows[0].Cells[0].Value = str4[1]; //Inserting value in first row
   dataGridView1.Rows[0].Cells[1].Value = str3[1]; //Inserting value in first row

   newVisitorVarRow = true; //if true add new rows
}

} //ENd of someforLoop()

__________________
A.Asif
 
Old October 24th, 2007, 03:27 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Firstly, dataGridView1.Rows.Add() returns an integer for the new row number.

so it should be:

int newRow = dataGridView1.Rows.Add()
dataGridView1.Rows[newRow].Cells[0].Value = str4[i];
dataGridView1.Rows[newRow].Cells[1].Value = str3[i];

I suspect this if the problem.

Normally however you don't add data directly to the datagrid but to the data source that it is bound too.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically add buttons to dataGrid in a placehold siliconvalley ASP.NET 2.0 Basics 0 October 26th, 2006 06:23 AM
How to add data to a DataGrid Cell. chiefouko VS.NET 2002/2003 2 July 28th, 2004 01:24 AM
Dynamically Add input tags Jstmehr4u3 Javascript How-To 2 July 29th, 2003 04:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.