我正在尝试构建一个允许使用文本区域进行批量SQL搜索的简单页面。文本框在每个新行上分开,每行作为查询参数。然后将生成的行添加到DataTable中。
查询没有问题,并且在通过调试模式检查时,我按照预期构建和返回DataTable。唯一的问题是当我尝试将DataTable绑定到gridview时,gridview没有行。
我现在一直在摸不着头脑,并且无法弄清楚为什么DataTable不会绑定。列名称全部匹配,但结果中的实际表未命名。这是一个问题吗?
这是代码。
ASPX:
<asp:GridView
ID="GridView1"
runat="server"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="SIM NUMBER" HeaderText="SIM NUMBER" />
<asp:BoundField DataField="Voice" HeaderText="Voice" />
<asp:BoundField DataField="IMSI" HeaderText="IMSI" />
<asp:BoundField DataField="Tariff" HeaderText="Tariff" />
<asp:BoundField DataField="Contract Start" HeaderText="Contract Start" />
<asp:BoundField DataField="Supplier" HeaderText="Supplier" />
</Columns>
</asp:GridView>
C#代码:
public partial class Query : System.Web.UI.Page
{
protected static string numbers = "";
protected static string number;
protected static DataTable result = new DataTable();
protected static string simNumber;
protected static string voice;
protected static string IMSI;
protected static string tariff;
protected static string contractStart;
protected static string supplier;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
result.Reset();
result.Columns.Add("SIM NUMBER", typeof(string));
result.Columns.Add("Voice", typeof(string));
result.Columns.Add("IMSI", typeof(string));
result.Columns.Add("Tariff", typeof(string));
result.Columns.Add("Contract Start", typeof(string));
result.Columns.Add("Supplier", typeof(string));
numbers = TextArea1.Value.ToString();
search();
GridView1.DataSource = result;
GridView1.DataBind();
}
protected static void search()
{
string[] split = numbers.Split(new string[] { "\r\n" }, StringSplitOptions.None);
foreach (string line in split)
{
if (line == string.Empty)
continue;
number = line;
getSupplierInfo();
}
}
protected static void getSupplierInfo()
{
DataTable DT = new DataTable();
using (SqlConnection conn = new SqlConnection(connections.supplierInfo()))
{
string sql = " SELECT * FROM UnionSuppliers WHERE SIM_NUMBER LIKE @parameter ";
conn.Open();
using (SqlCommand select = new SqlCommand
{
CommandType = CommandType.Text,
CommandTimeout = 300,
CommandText = sql,
Connection = conn
})
{
select.Parameters.AddWithValue("@parameter", number);
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
adapter.SelectCommand = select;
adapter.Fill(ds);
DT = ds.Tables[0];
}
}
foreach (DataRow dr in DT.Rows)
{
simNumber = dr["SIM_NUMBER"].ToString();
voice = dr["Voice"].ToString();
IMSI = dr["IMSI"].ToString();
tariff = dr["Tariff"].ToString();
contractStart = dr["Contract_start"].ToString();
supplier = dr["Supplier"].ToString();
addRow();
}
}
protected static void addRow()
{
DataRow simResult = result.NewRow();
simResult["SIM Number"] = simNumber;
simResult["Voice"] = voice;
simResult["IMSI"] = IMSI;
simResult["Tariff"] = tariff;
simResult["Contract Start"] = contractStart;
simResult["Supplier"] = supplier;
result.Rows.Add(simResult);
}
}
非常感谢任何帮助。我确信它非常简单,我不知道。
由于
答案 0 :(得分:0)
尝试更改:
AutoGenerateColumns="false"
为:
AutoGenerateColumns="true"
答案 1 :(得分:0)
我认为你可以在aspx文件中尝试这样做
<Columns>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Your_Field_Name") %>'></asp:Label>
</ItemTemplate>
</Columns>
并且在cs文件中恰好适合代码
DataTable DT = new DataTable();
using (SqlConnection conn = new SqlConnection(connections.supplierInfo()))
{
string sql = " SELECT * FROM UnionSuppliers WHERE SIM_NUMBER LIKE @parameter ";
conn.Open();
using (SqlCommand select = new SqlCommand
{
CommandType = CommandType.Text,
CommandTimeout = 300,
CommandText = sql,
Connection = conn
})
{
select.Parameters.AddWithValue("@parameter", number);
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
adapter.SelectCommand = select;
adapter.Fill(ds);
DT = ds.Tables[0];
}
}
现在将Datatable绑定到Grid View。
GridView1.DataSource = DT;
GridView1.DataBind();