Anonymous
Данные текстового поля в Gridview
Сообщение
Anonymous » 07 ноя 2024, 23:51
Я пытаюсь выполнить приведенный ниже код, но смотрите скриншот.
Код: Выделить всё
private void CopyTextBoxToDataGridView()
{
// Split the lines from the textBox1 text
string[] lines = textBox1.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
if (dataGridView1.Columns.Count == 0)
{
dataGridView1.Columns.Add("Column1", "Column 1");
dataGridView1.Columns.Add("Column2", "Column 2");
dataGridView1.Columns.Add("Column3", "Column 3");
}
dataGridView1.Rows.Clear();
for (int i = 0; i < lines.Length; i++)
{
string[] splitLine = lines[i].Split(new char[] { '-', ']' });
string col1 = splitLine.Length > 0 ? splitLine[0] : string.Empty;
string col2 = splitLine.Length > 1 ? splitLine[1] : string.Empty;
string col3 = splitLine.Length > 2 ? splitLine[2] : string.Empty;
// Check if the next line starts with '['
if (i + 1 < lines.Length && lines[i + 1].StartsWith("["))
{
col3 = "1"; // Set "1" in column 3
}
dataGridView1.Rows.Add(col1.Replace("[", "").Replace("]", ""), col2.Replace("[", "").Replace("]", ""), col3);
}
}
Вывод:
Если при вводе текстового поля не применяется время, например 2, то по умолчанию в столбце 3 должно быть 1.
Подробнее здесь:
https://stackoverflow.com/questions/791 ... o-gridview
1731012666
Anonymous
Я пытаюсь выполнить приведенный ниже код, но смотрите скриншот. [code]private void CopyTextBoxToDataGridView() { // Split the lines from the textBox1 text string[] lines = textBox1.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.None); if (dataGridView1.Columns.Count == 0) { dataGridView1.Columns.Add("Column1", "Column 1"); dataGridView1.Columns.Add("Column2", "Column 2"); dataGridView1.Columns.Add("Column3", "Column 3"); } dataGridView1.Rows.Clear(); for (int i = 0; i < lines.Length; i++) { string[] splitLine = lines[i].Split(new char[] { '-', ']' }); string col1 = splitLine.Length > 0 ? splitLine[0] : string.Empty; string col2 = splitLine.Length > 1 ? splitLine[1] : string.Empty; string col3 = splitLine.Length > 2 ? splitLine[2] : string.Empty; // Check if the next line starts with '[' if (i + 1 < lines.Length && lines[i + 1].StartsWith("[")) { col3 = "1"; // Set "1" in column 3 } dataGridView1.Rows.Add(col1.Replace("[", "").Replace("]", ""), col2.Replace("[", "").Replace("]", ""), col3); } } [/code] Вывод: [img]https://i.sstatic.net/rUBQv0Wk.jpg [/img] Если при вводе текстового поля не применяется время, например 2, то по умолчанию в столбце 3 должно быть 1. Подробнее здесь: [url]https://stackoverflow.com/questions/79150029/textbox-data-to-gridview[/url]