Inserting MatchCollection into datagridview c#
I have a datagridview that has 2 columns and a MatchCollection that I will be using to fill the datagrid. How can i insert into the datagrid for the first column the first match in matchcollection then for the second column the second value of matchcollection. It will then create a new row and start again.
No data is bound to this gridview and I need to ensure that the matchCollection that is being inserted into the datagrid does not overwrite anything else in the table. How can i do this?
This is done in forms application not asp.net
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
line = line.Trim();
if (line.StartsWith("addTestingPageContentText"))
{
string temp;
string pattern = "\"([^\"]+)\"";
Regex r = new Regex(pattern);
MatchCollection regs = r.Matches(line);
foreach (Match reg in regs)
{
temp = reg.ToString();
temp = temp.Replace("\"", "");
int rowCount = contentTable_grd.Rows.Count - 1;
if (contentTable_grd.Rows[rowCount].Ce开发者_StackOverflowlls[0].Value == null)
contentTable_grd.Rows[rowCount].Cells[0].Value = temp;
else
contentTable_grd.Rows[rowCount].Cells[1].Value = temp;
contentTable_grd.Rows.Add();
}
}
}
try this
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
line = line.Trim();
if (line.StartsWith("addTestingPageContentText"))
{
string temp;
string pattern = "\"([^\"]+)\"";
Regex r = new Regex(pattern);
MatchCollection regs = r.Matches(line);
object[] array1 = new object[2];
foreach (Match reg in regs)
{
temp = reg.ToString();
temp = temp.Replace("\"", "");
if (array1[0] == null)
array1[0] = temp;
else
array1[1] = temp;
}
if (regs.Count > 0)
contentTable_grd.Rows.Add(array1);
}
}
You can create custom gridview column and inherit from DataGridViewTextBoxCell.
Host Controls in Windows Forms DataGridView Cells
精彩评论