为什么SqlDataAdapter的填充不允许我添加一行,其行的值与外观相同?

时间:2015-03-20 08:32:47

标签: c# datagridview sqldataadapter

为什么SqlDataAdapter的Fill方法不允许我添加一行,其行的值与外观相同?我无法成功提供与DataTable中填充的行相同的行显示的行值。  Check this out:

using (SqlDataAdapter a = new SqlDataAdapter("SELECT SIPNO, SERINO, TARIH FROM SNOHAREKETLER WHERE Cast(TARIH as DATE) BETWEEN '2015/03/19' AND '2015/03/20' AND (TEZNO = 'T23' OR TEZNO = 'T31') AND CIKTI is null", c))
            {
                // 3
                // Use DataAdapter to fill DataTable
                DataTable t = new DataTable();
                a.Fill(t);

                t.Columns.Add("MyColumn", typeof(string));

                DataRow workRow;

                int iGetCount = t.Rows.Count;

                for (int i = 0; i <= iGetCount - 1; i++)
                {
                    workRow = t.NewRow();
                    workRow["MyColumn"] = i;
                    t.Rows.Add(workRow);
                }

                // 4
                // Render data onto the screen
                dataGridView1.DataSource = t;
            }

1 个答案:

答案 0 :(得分:0)

以下是我的问题的解决方案。我搜索并咨询了某人。我的问题是尝试添加NewRow,这会导致问题。

DataTable t = new DataTable();
a.Fill(t);

DataColumn newCol = new DataColumn("NewColumn", typeof(string));
newCol.AllowDBNull = true;
t.Columns.Add(newCol);
foreach (DataRow row in t.Rows)
{
     row["NewColumn"] = "With String";
}
dataGridView1.DataSource = t;