2

I have a string that contains: "# of rows, # of columns, Row'X'Col'X'=Serial#, ...

How do I create a DataGrid table with the number of rows and columns defined, and then place the serial #s into the grid.

Examples:

2,1,R1C1=111,R2C1=112,

2,2,R1C1=211,R1C2=212,R2C1=213,R2C2=214,

thanks

6
  • I am not really understanding what your asking here: then the rest of the variables to input into the correct cells without hardcoding each one ? Commented Oct 4, 2016 at 1:08
  • Are you asking to put each PLX into the order of 60 in the top left, 62 bottom left, 61 in top right, and 63 in bottom right? If so use the numbers and use a multiple of 4 in your algorithm. Commented Oct 4, 2016 at 1:15
  • It looks like a simple string.split.. on the input string to get the RXCX values. Then you would know the dimensions needed for the grid. Commented Oct 4, 2016 at 3:48
  • Hello Timm The dimensions are in the 2nd and 3rd values of the input string… After looking at your problem description I have a questions/suggestion. It seems odd that your input string would have indexes for the data as shown. Basically the data is saying “You WILL construct the table in this fashion.” If the data is used for “MY” purposes, I would ignore the RXCX altogether and construct the table the way that best suits what I want to accomplish. Commented Oct 4, 2016 at 4:34
  • John, I simplified my question, does that answer your question? Commented Oct 4, 2016 at 5:26

1 Answer 1

1

Below is code that does what you are asking; however I must point out some problems with this approach. First, getting the total rows and cols from the first two elements in order to create your table is risky. If that data is wrong, this code will most likely crash or possibly omit data. Example if the input is: 2,2,RXCX=.., RXCX=.., RXCX=.., RXCX=..,RXCX=, RXCX=… This line will only get the first 4 values.

Worse… this will crash… if the input is 2,2,RXCX=.., RXCX=.. Then it will crash when you try to access the 4th element in the splitArray because there isn’t a 4th element. Either way is not good.

My point is to be safe… it would be a better approach to see how much data is actually there before you create the grid. You could get how many items there are with StringArray.Length minus the first two elements. These elements will define the dimensions and allow you to check their validity. This will make sure your loops won’t go out of bounds because the supplied data was wrong. It seems redundant and error prone to supply the dimension values when you can get that info from the data itself.

I still am not 100% sure what you want to accomplish here. It looks like a search of some form. This is what I am picturing… Looking at your (previous) screen shots it appears to me that after you type into the Serial # text box and click the “Search Txt Files” button it will search for data that came from the input string i.e. “PLX51…” and then have the grid display the “filtered” results that match (or are LIKE) what’s in the Serial # textbox. If this is true, I would ignore the RXCX vales and put the data in a single column. Then wire up an OnKeyPress event for the text box to filter the grid whenever the user types into the Serial # text box. Otherwise I am lost as to why you would need to create the data in the fashion described. Just because the input has unnecessary data… doesn’t mean you have to use it. Just a thought.

  string inputString = "2,2,R1C1=211,R1C2=212,R2C1=213,R2C2=214";
  string[] splitArray = inputString.Split(',');
  int totalRows = int.Parse(splitArray[0]);
  int totalCols = int.Parse(splitArray[1]);
  int itemIndex = 2;

  // add the columns
  for (int i = 0; i < totalCols; i++)
  {
    dataGridView1.Columns.Add("Col", "Col");
  }
  // add the rows
  dataGridView1.Rows.Add(totalRows);


  for (int i = 0; i < totalRows; i++)
  {
    for (int j = 0; j < totalCols; j++)
    {
      dataGridView1.Rows[i].Cells[j].Value = splitArray[itemIndex];
      itemIndex++;
    }
  }
Sign up to request clarification or add additional context in comments.

5 Comments

This is Great! is it possible to invert the rows so that row1 items are in the bottom row, and the greatest numbered row items are in the top row? Also with this method how would I print only the last 5 characters of the value into the cell?
You want the Cells to look like 1=211 or 2=212 ?
well, those were simplified examples, the numbers are normally 8-12 digits, but I only want to display that last 4 or 5 digits. I have already split the "RXCX =" out by splitting the string (',' , '=') and increasing the index to two.
That clears up some things for me... since you have already split the string on '=' then you should have the whole string and it could be any length. A simple substring of that would give you the first 4-5 characters or checking if its less than 4-5 characters. If that is what you want to do then I do not understand why you would not do this when you initially read the data into the array. Why put the RXCX values in the data array if you are going to remove them on output? Like I said the RXCX values can be determined by "where" the cell is located in the DataGrid. I hope that makes sense.
Yes, you have been an extreme help, Thanks JohnG!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.