Original Post
Hi, I am writing a program that uses DataGrids. In C# it seems very easy to set the name of the column, but I cant figure out how to set the name of the Row (that gray space on the left hand side of each row). The code that I am using to create the DataGrid is as follows So thats it. Is there any way that I can set the name of the row. The reason that I want to set it is that each column representes a question from 1-10, then each row is a multiple of 10, eg 1-10 11-20 21-30 31-40 etc.... Is is this info that I want to put in the side of the DataGrid. Thanks Ciaran [Edited by - giant on July 17, 2004 7:51:19 AM]
DataTable dt = new DataTable();
DataRow dr;
//Add the Columns
DataColumn dc = new DataColumn("Question", typeof(string));
//Make the first Column ReadOnly
dc.ReadOnly = true;
dt.Columns.Add( dc );
dt.Columns.Add(new DataColumn("1", typeof(string)));
dt.Columns.Add(new DataColumn("2", typeof(string)));
dt.Columns.Add(new DataColumn("3", typeof(string)));
dt.Columns.Add(new DataColumn("4", typeof(string)));
dt.Columns.Add(new DataColumn("5", typeof(string)));
dt.Columns.Add(new DataColumn("6", typeof(string)));
dt.Columns.Add(new DataColumn("7", typeof(string)));
dt.Columns.Add(new DataColumn("8", typeof(string)));
dt.Columns.Add(new DataColumn("9", typeof(string)));
dt.Columns.Add(new DataColumn("10", typeof(string)));
//Convert the Access Answers to an Array which we can index into
string Answers = dataGridAnswers[0, 2].ToString();
char[] cArray = Answers.ToCharArray( 0, Answers.Length );
//Fill in all Rows
int iQuestionNumber = 0;
//Keep track of where we are
int iRowNumber = 0;
int iColumnNumber = 1;
//How many questions are there
iNumberOfQuestions = Convert.ToInt32( dataGridAnswers[0, 1] );
//Create the First Row
dr = dt.NewRow();
//Fill in the info for each of the questions
for (int i = 0; i < Answers.Length; i++)
{
//Only Create a new row if we have processed 10 questions
if( iColumnNumber == 1 )
{
//Create the new row
dr = dt.NewRow();
//We have back at the first column
iColumnNumber = 1;
//But have gone onto the next row
iRowNumber++;
//Set the Text of the Question Number Column
dr[0] = Convert.ToString( iQuestionNumber + 1 ) + " - " + Convert.ToString( iQuestionNumber + 10 );
//dr.Table.Rows. = "Temp";
}
//What Question are we currently on
iQuestionNumber = i + 1;
//Add the Answer
if( i < iNumberOfQuestions )
{
//Fill in the Correct Answer
dr[iColumnNumber] = cArray.ToString();
}
else
{
dr[iColumnNumber] = "?";
}
//Go to the next Column
iColumnNumber++;
//If we have reached the end of the table then add the row
//and go back to the start of the Columns
if( iColumnNumber == 11 )
{
dt.Rows.Add(dr);
iColumnNumber = 1;
}
}