如何将可编辑的GridView转换为CSV [英] How to convert the editable gridview into csv

查看:75
本文介绍了如何将可编辑的GridView转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,


我在gridview中有一个疑问.我从数据源中获取值并将其放入具有insert,update,delete操作的gridview中.之后,我想将此gridview转换为csv格式.发生在gridview中来了.其他字段不来了.下面我已经发送了该代码.plz对其进行纠正.

//Default.aspx//

Hai,


I have a doubt in gridview.I get the values from the datasource and put it in a gridview with insert,update,delete operations.After that i want to convert this gridview into csv format.I tried but I got only the heading field that occured in gridview is coming.Other fields are not coming.Below I has send that code.plz rectify it.

//Default.aspx//

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            
            onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
            onrowupdating="GridView1_RowUpdating">
            <columns>
              <asp:CommandField HeaderText="Edit-Update" ShowEditButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" />
                <asp:BoundField DataField="NAME" HeaderText="NAME" />
                <asp:BoundField DataField="AGE" HeaderText="AGE" />
                <asp:BoundField DataField="PLACEOFBIRTH" HeaderText="PLACEOFBIRTH" />
                <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
               

            </columns>
        
         
        <br />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Export to csv" />
         
    </div>
    </form>
</body>
</html>


//Default.aspx.cs//


//Default.aspx.cs//

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            binddata();
        }
    }

    private void binddata()
    {
        SqlConnection con = new SqlConnection("Data Source=MACFEESERVER;Initial Catalog=ePO_LENOVO;User ID=sa;Password=sa");
        SqlDataAdapter ada = new SqlDataAdapter("select ID,NAME,AGE,PLACEOFBIRTH from RAJ", con);
        DataTable dt = new DataTable();
        ada.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=MACFEESERVER;Initial Catalog=ePO_LENOVO;User ID=sa;Password=sa");
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "delete from RAJ where ID=@ID";
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        binddata();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        binddata();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        binddata();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text == "Insert")
        {
            SqlConnection con = new SqlConnection("Data Source=MACFEESERVER;Initial Catalog=ePO_LENOVO;User ID=sa;Password=sa");
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "insert into RAJ(NAME,AGE,PLACEOFBIRTH)values(@NAME,@AGE,@PLACEOFBIRTH)";
            cmd.Parameters.Add("@NAME", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[2].Controls[0]).Text;
            cmd.Parameters.Add("@AGE", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[0].Cells[3].Controls[0]).Text;
            cmd.Parameters.Add("@PLACEOFBIRTH", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[4].Controls[0]).Text;
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        else
        {
            SqlConnection con = new SqlConnection("Data Source=MACFEESERVER;Initial Catalog=ePO_LENOVO;User ID=sa;Password=sa");
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText ="update RAJ set NAME=@NAME,AGE=@AGE,PLACEOFBIRTH=@PLACEOFBIRTH where ID=@ID";
            cmd.Parameters.Add("@NAME", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            cmd.Parameters.Add("@AGE", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            cmd.Parameters.Add("@PLACEOFBIRTH", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
            cmd.Parameters.Add("@ID",SqlDbType.Int).Value =Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

        }
        GridView1.EditIndex = -1;
        binddata();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=MACFEESERVER;Initial Catalog=ePO_LENOVO;User ID=sa;Password=sa");
        SqlDataAdapter ada = new SqlDataAdapter("select ID,NAME,AGE,PLACEOFBIRTH from RAJ", con);
        DataTable dt = new DataTable();
        ada.Fill(dt);
        DataRow dr = dt.NewRow();
        dt.Rows.InsertAt(dr, 0);
        GridView1.EditIndex = 0;
        GridView1.DataSource = dt;
        GridView1.DataBind();
        ((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
        Response.Charset = "";
        Response.ContentType = "application/text";





        GridView1.AllowPaging = false;
        GridView1.DataBind();

        StringBuilder sb = new StringBuilder();
        for (int k = 0; k < GridView1.Columns.Count; k++)
        {
            //add separator
            sb.Append(GridView1.Columns[k].HeaderText + '','');
        }
        //append new line
        sb.Append("\r\n");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            for (int k = 0; k < GridView1.Columns.Count; k++)
            {
                //add separator
                sb.Append(GridView1.Rows[i].Cells[k].Text + '','');
            }
            //append new line
            sb.Append("\r\n");
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.End();

    }
}

推荐答案

您需要将数据导出到csv文件.请参见此处 [
You need to export data to a csv file. See here[^].


在代码项目中讨论了此主题.我希望CSV也有相同的方法.

这种方法似乎很简单(Soni Uma的答案).我尚未测试,所以效果不好.

将Gridview导出到ASP.NET中的Excel [ ^ ]
This topic is discussed here in code project. I hope there is same method for CSV as well.

This method seems to be simple (Answer by Soni Uma). I haven''t tested, so I am not it works well.

Export Gridview to Excel in ASP.NET[^]


这篇关于如何将可编辑的GridView转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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