如何在c#gridview中使用的查询中转换行中的列? [英] How to turn columns in rows in a query used in c# gridview ?

查看:71
本文介绍了如何在c#gridview中使用的查询中转换行中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在我使用ac#gridview查询的情况下如何在行中转换列,并且行中的值正在改变?

How to turn columns in rows in a case where i use a query for a c# gridview , and the value in rows are changing ?

例如我有:

我想要这个结果:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Net.Mail;

public partial class Incident_POS_ok : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Server= ????; Database= ????; Integrated Security=True;");
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string str = @"select  MerchantName as Nume_Comerciant ,Outlet.Name as Nume_Magazin ,POSProducerName,POSEntityRON AS Terminal_id ,
		LabelEncryptionKey as SeriePOS_PinPad , SoftVersionID,
	    Outlet.Address as Adresa_Magazin , AddressNumber as Nr, AddressLocalitate as Localitate, 
		m_Judet_1.JudetName as judetOutlet,SIMSeries
            
from POS 
            inner join Merchant on POS.MerchantID = Merchant.MerchantID 
            inner join Outlet on Merchant.MerchantID = Outlet.MerchantID
            INNER JOIN dbo.Department ON dbo.POS.DepartmentID = dbo.Department.DepartmentID
            AND dbo.Merchant.MerchantID = dbo.Department.MerchantID 
			AND dbo.Outlet.OutletID = dbo.Department.OutletID 
			inner join m_POSProducer on POS.POSProducerID = m_POSProducer.POSProducerID
			inner join m_Judet on Merchant.JudetID = m_Judet.JudetID and 
			Outlet.MerchantID = Merchant.MerchantID
			inner join m_Judet AS m_Judet_1 ON Outlet.AddressJudetId = m_Judet_1.JudetID
where ( POSEntityRON like '%' + @search + '%')";

        SqlCommand xp = new SqlCommand(str, con);
        xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text;

        con.Open();
        xp.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = xp;
        DataSet ds = new DataSet();
        da.Fill(ds, "Name");
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    private string GridViewToHtml(GridView grid)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        grid.RenderControl(hw);
        return sb.ToString();

    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string Mail_From = "email";
        string password = "password";
        string Mail_Subject = "Incident POS";
        string Mail_TO = "email";
        string Mail_Body = GridViewToHtml(GridView1) + " " + "Telefon BT : 0040 Daniel Tou" + "  " + " ; " + "Telefon in locatie :" + "   " + TextBox3.Text + "   " + " ; " + " Motiv Incident: " + " " + TextBox2.Text;


        MailMessage MM = new MailMessage(Mail_From, Mail_TO, Mail_Subject, Mail_Body);
        MM.IsBodyHtml = true;
        SmtpClient sc = new SmtpClient();
        sc.Host = "Smtp.btrl.ro";
        sc.Port = xxxx;
        sc.Credentials = new System.Net.NetworkCredential(Mail_From, password);
        sc.EnableSsl = true;
        sc.Send(MM);

        ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert ('E-mail trimis !!!');", true);




推荐答案

嗨Danie,



如果我是的话请纠正我错了,从我看到的问题更多的是关于GridView,而不是SQL查询,对吧?如果是这样,我建议你看看这个

GridView旋转样本
看看是否有帮助。 



另外如果问题与GridView更相关,我建议你将它发布在

Web窗体数据控制论坛
以获得更好的响应。



如果您有任何其他问题,请告诉我。$


问候,b
Lin
Hi Danie,

Please correct me if I’m wrong, from what I see the question is more about GridView, not the SQL query, right? If so, I would suggest take a look into this GridView rotate sample see if helps. 

Also, if the question is more related to GridView, I would suggest you post it in Web Forms Data Controls forum for better response.

If you have any other questions, please let me know.

Regards,
Lin


这篇关于如何在c#gridview中使用的查询中转换行中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆