Excel upload to gridview in asp.net C#

RAVI 1,056 Reputation points
2023-05-11T17:17:34.9233333+00:00

Hello

I used this code to copy excel and paste in textbox and it shows perfect in gridview but i want country data column should be default value to comes once only ID,Name from excel and i paste it textbox for example county name IND how to change this code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
    <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<br />
<asp:TextBox ID="txtCopied" runat="server" TextMode="MultiLine" AutoPostBack="true"
OnTextChanged="PasteToGridView" Height="200" Width="400" />

C# Code

protected void PasteToGridView(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                    new DataColumn("Name", typeof(string)),
                    new DataColumn("Country",typeof(string)) });
 
    string copiedContent = Request.Form[txtCopied.UniqueID];
    foreach (string row in copiedContent.Split('\n'))
    {
        if (!string.IsNullOrEmpty(row))
        {
            dt.Rows.Add();
            int i = 0;
            foreach (string cell in row.Split('\t'))
            {
                dt.Rows[dt.Rows.Count - 1][i] = cell;
                i++;
            }
        }
    }
    GridView1.DataSource = dt;
    GridView1.DataBind();
    txtCopied.Text = "";
}

i want country data column should be default value to comes once only ID,Name from excel and i paste it textbox for example county name IND how to change this code

Thanking You

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,507 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,751 Reputation points Microsoft Vendor
    2023-05-12T04:49:19.5+00:00

    Hi @RAVI,

    You mean that when there is only Id and name in the Excel sheet but no country, you want the country to default to: IND.

    You can try the following code:

    protected void PasteToGridView(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Country",typeof(string)) });          
                string copiedContent = Request.Form[txtCopied.UniqueID];
                foreach (string row in copiedContent.Split('\n'))
                {
                    if (!string.IsNullOrEmpty(row))
                    {
                        dt.Rows.Add();
                        int i = 0;
                        foreach (string cell in row.Split('\t'))
                        {
                            dt.Rows[dt.Rows.Count - 1][i] = cell;
                            i++;
                            
                        }
                      
                    }
                }
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    for (int k = 0; k < dt.Columns.Count; k++)
                    {
                        if (dt.Rows[j][k].ToString() == "\r")
                        {
                            dt.Rows[j][k] = "IND";
                        }
                    }
                }            
                GridView1.DataSource = dt;
                GridView1.DataBind();
                txtCopied.Text = "";
            }
    

    enter image description here If you want to set the country column as a default value, you can use the DefaultValue property.

       string strColName = "Country";
                DataColumn colNew = new DataColumn(strColName, typeof(string));
                colNew.DefaultValue = "IND";
                dt.Columns.Remove(strColName);
                dt.Columns.Add(colNew);
    

    User's image

    User's image

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.