我试图解决问题,但无法解决相同的列名称,但它无法正常工作。怎么解决?
private void GetData()
{
try
{
dt.Columns.Clear();
dt.Rows.Clear();
char[] alignment = new char[22];
int[] width = new int[22];
dt.Columns.Add("S.No", typeof(int)); alignment[0] = 'C'; width[0] = 20;
dt.Columns.Add("Ad.No", typeof(int)); alignment[1] = 'C'; width[1] = 20;
dt.Columns.Add("Gr No", typeof(string)); alignment[2] = 'L'; width[2] = 60;
//dt.Columns.Add("Father Name", typeof(string));
dt.Columns.Add("Ad. Date", typeof(string)); alignment[3] = 'L'; width[3] = 30;
dt.Columns.Add("[Student Name]", typeof(string)); alignment[4] = 'C'; width[0] = 20;
dt.Columns.Add("Father Name", typeof(string)); alignment[5] = 'C'; width[0] = 20;
string query = "";
if (radioButtonClass.Checked)
{
query = " select ROW_NUMBER() OVER(ORDER BY Class,StudentName) AS [S.No], AdmissionNo as [Ad.No], EnrollmentNo as [Gr No],convert(varchar,AdmissionDate,103)as [Ad.Date],StudentName as [Student Name],FatherName as [Father Name], " +
" from vu_StdAdmissionFeeInfo " +
" where AdmissionDate >='" + dtpform.Value + "' and AdmissionDate<='" + dtpTo.Value + "'" +
"" + (cmbClass.Text.Equals("--Select--") ? "" : " and Class='" + cmbClass.Text + "' ") +
"" + (cmbsection.Text.Equals("--Select--") ? "" : " and Section='" + cmbsection.Text + "' ") +
" order by Class,StudentName";
}
DataTable dt2 = new DataTable();
dt2 = utility.Get(query);
// dt2.Clear();
foreach (DataRow item in dt2.Rows)
{
DataRow dr = dt.NewRow();
dr["S.NO"] = item["S.NO"];
dr["Ad.No"] = item["Ad.No"];
dr["Gr No"] = item["Gr No"];
dr["Ad. Date"] = item["Ad. Date"];
dr["[Student Name]"] = item["[Student Name]"];
dr["Father Name"] = item["Father Name"];
dt.Rows.Add(dr);
}
dt2.Rows.Clear();
// dt.Rows.Clear();
if (dt.Rows.Count > 0)
{
dgvReport.DataSource = null;
dgvReport.DataSource = dt;
}
else
{
dgvReport.DataSource = null;
}
}
}
}
答案 0 :(得分:3)
在以下代码中删除 [学生姓名] 的括号:
foreach (DataRow item in dt2.Rows)
{
DataRow dr = dt.NewRow();
dr["S.NO"] = item["S.NO"];
dr["Ad.No"] = item["Ad.No"];
dr["Gr No"] = item["Gr No"];
dr["Ad. Date"] = item["Ad. Date"];
dr["[Student Name]"] = item["[Student Name]"];
dr["Father Name"] = item["Father Name"];
dt.Rows.Add(dr);
}
或此代码:
dt.Columns.Add("[Student Name]", typeof(string)); alignment[4] = 'C'; width[0] = 20;
或者
删除列的空格