我正在使用此Linq查询在gridview中显示列。
protected void Page_Load(object sender, EventArgs e)
{
string CS = ConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(CS);
SqlCommand command = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
try
{
connection.Open();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "ManLog_Profits";
adapter = new SqlDataAdapter(command);
adapter.Fill(dt);
adapter.Fill(dt1);
var query = from r in dt.AsEnumerable()
where r.Field<string>("Department") == "Manufacturing"
select new
{
ID = r["ID"].ToString(),
Dep_Name = r["Dep Name"].ToString(),
Sales_Profit = r["Sales Profit"].ToString(),
Sales_Transport = r["Sales Transport"].ToString (),
};
GridView1.DataSource = query;
GridView1.DataBind();
}
}
现在我怎么可能删除列名的下划线。
答案 0 :(得分:1)
在您的linq查询中使用匿名类型。您可以自己决定名称
select new
{
ID = r["ID"].ToString(),
DepName = r["Dep Name"].ToString(),
SalesProfit = r["Sales Profit"].ToString(),
SalesTransport = r["Sales Transport"].ToString()
};
答案 1 :(得分:0)
设置列manualy - AutoGenerateColumns = false
:
GridView1.AutoGenerateColumns = false;
GridView1.Columns.Add(new BoundField() { HeaderText = "ID", DataField = "ID" });
GridView1.Columns.Add(new BoundField() { HeaderText = "Dep Name", DataField = "Dep_Name" });
GridView1.Columns.Add(new BoundField() { HeaderText = "Sales Profit", DataField = "Sales_Profit" });
GridView1.Columns.Add(new BoundField() { HeaderText = "Sales Transport", DataField = "Sales_Transport" });
GridView1.DataBind();
您也可以使用DataView.Filter代替Linq查询:
DataView dv = new DataView() {Table = dt};
dv.Filter = "[Department] = 'Manufacturing'"
GridView1.DataSource = dv;
GridView1.DataBind();