在gridview中如何通过动态创建的列设置列名? [英] In gridview how to set column name by dynamically created column?
问题描述
在我的输出中,动态创建了一列,该列名是柱下输出中的column1(5)。我想设置该列名。
< b>我尝试了什么:
这是标记代码
< ; asp:GridView ID = GridView1 runat = server AutoGenerateColumns = true 可见 = true >
< / asp:GridView >
这是我的代码
受保护 void Button1_Click(对象发件人,EventArgs e)
{
SqlCommand cmd = new SqlCommand( 选择S1_DateTime,S1_Weight,S2_Weight,AdjustmentWeight,ABS(convert(float,S1_Weight)-convert(float,S2_Weight)-convert(float,AdjustmentWeight))from dbo_OrderLines1 where convert(varchar(25),S1_DateTime,126),如'% + Calendar1.SelectedDate.ToString( yyyy-MM-dd)+ %',con1) ;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Label1.Text = 行数= + dt.Rows.Count.ToString ();
TextBox1.Text = Calendar1.SelectedDate.ToString( yyyy-MM-dd) ;
GridView1.Visible = true ;
GridView1.DataSource = dt;
GridView1.DataBind();
}
这是输出表的这一列的输出1.S1_DateTime 2.S1_Weight 3.S2_Weight 4。 AdjustmentWeight 5.Column1,我需要设置column1(5)名称怎么做?
你可以通过在查询中给它一个别名来设置列名:
... ABS(...) As YourColumnName ...
而不是将datetime
列转换为字符串,测试它是否大于或等于所选日期,并且在所选日期之后不到一天。
S1_DateTime> = @ SelectedDate 和 S1_DateTime< DateAdd(日, 1 , @ SelectedDate )
使用包裹你的
语句。SqlCommand
和SqlConnection
对象
并使用参数化查询来避免 SQL注入 [ ^ ]。
使用(SqlConnection con = new SqlConnection( ...))
使用(SqlCommand cmd = 新 SqlCommand( 选择S1_DateTime,S1_Weight,S2_Weight,AdjustmentWeight,ABS(转换(float,S1_Wei) ght) - 转换(float,S2_Weight) - convert(float,AdjustmentWeight))作为来自dbo_OrderLines1的YourColumnName,其中S1_DateTime> = @SelectedDate和S1_DateTime< DateAdd(day,1,@ SelectedDate),con))
{
cmd.Parameters.AddWithValue( @ SelectedDate,Calendar1.SelectedDate);
SqlDataAdapter da = new SqlDataAdapter(cmd );
DataTable dt = new DataTable();
da.Fill(dt);
Label1.Text = 行数= + dt.Rows.Count.ToString();
TextBox1.Text = Calendar1.SelectedDate.ToString( yyyy-MM-dd);
GridView1.Visible = true ;
GridView1.DataSource = dt;
GridView1.DataBind();
}
In my output one column in dynamically created, that column name is column1(5) in the bellow output.i want to set that column name.
What I have tried:
This is markup code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" Visible="true">
</asp:GridView>
this is my code
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand(" select S1_DateTime,S1_Weight,S2_Weight,AdjustmentWeight,ABS(convert(float,S1_Weight)-convert(float,S2_Weight)-convert(float,AdjustmentWeight)) from dbo_OrderLines1 where convert(varchar(25),S1_DateTime,126) like '%" + Calendar1.SelectedDate.ToString("yyyy-MM-dd") + "%'", con1);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Label1.Text ="number of rows = "+ dt.Rows.Count.ToString();
TextBox1.Text = Calendar1.SelectedDate.ToString("yyyy-MM-dd");
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
this is ouptput coming like this columns of the output table 1.S1_DateTime 2.S1_Weight 3.S2_Weight 4.AdjustmentWeight 5.Column1,i need to set the column1(5) name how to do?
You can set the column name by giving it an alias within the query:
... ABS(...) As YourColumnName ...
Rather than converting yourdatetime
column to a string, test whether it is greater than or equal to the selected date, and less than one day after the selected date.
S1_DateTime >= @SelectedDate And S1_DateTime < DateAdd(day, 1, @SelectedDate)
Wrap yourSqlCommand
andSqlConnection
objects inusing
statements.
And use parameterized queries to avoid SQL Injection[^].
using (SqlConnection con = new SqlConnection("...")) using (SqlCommand cmd = new SqlCommand(" select S1_DateTime, S1_Weight, S2_Weight, AdjustmentWeight, ABS(convert(float, S1_Weight) - convert(float, S2_Weight) - convert(float, AdjustmentWeight)) As YourColumnName from dbo_OrderLines1 where S1_DateTime >= @SelectedDate And S1_DateTime < DateAdd(day, 1, @SelectedDate)", con)) { cmd.Parameters.AddWithValue("@SelectedDate", Calendar1.SelectedDate); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); Label1.Text ="number of rows = "+ dt.Rows.Count.ToString(); TextBox1.Text = Calendar1.SelectedDate.ToString("yyyy-MM-dd"); GridView1.Visible = true; GridView1.DataSource = dt; GridView1.DataBind(); }
这篇关于在gridview中如何通过动态创建的列设置列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!