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 = "";
}
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);
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.