Paul Rajs

Paul Rajs

  • NA
  • 641
  • 148.3k

How to bind column values dynamically from datatable using c#

Jul 1 2021 1:59 PM

Hi Developers,

I have done the generate hyperlink column dynamically and working fine as get Column Names from database.

However suppose some one change the naming convension on database sp it will not work or throw error.

So now am need to bind the respective Column Values Dynamically.am using the following C# code

XmlNodeList DataClassWiseInventoryDetails = doc.GetElementsByTagName("GetClassWiseInventoryDetails");
DataTable dtDetails = Dynamic.ConvertXmlNodeListToDataTable(DataClassWiseInventoryDetails);
foreach (DataRow drDetails in dtDetails.Rows)
{
    // Current Output
    drDetails[dtDetails.Columns[0].ColumnName.ToString()] = "<a style='color:blue' href='#' onclick='return ViewBasicDetails(&quot;" + drDetails["Vehicle_Type"] + "," + drDetails["CLASS_ID"] + "," + 2 + "&quot;);' title='Click To View'>" + drDetails["Vehicle_Type"] + "</a>";
    drDetails[dtDetails.Columns[1].ColumnName.ToString()] = "<a style='color:blue' href='#' onclick='return ViewBasicDetails(&quot;" + drDetails["Total_Tags"] + "," + drDetails["CLASS_ID"] + "," + 2 + "&quot;);' title='Click To View'>" + drDetails["Total_Tags"] + "</a>";

    // Expected Output
    drDetails[dtDetails.Columns[0].ColumnName.ToString()] = "<a style='color:blue' href='#' onclick='return ViewBasicDetails(&quot;" + BindColumnNameValueDynamically + "," + BindColumnNameValueDynamically + "," + 6 + "&quot;);' title='Click To View'>" + BindColumnNameValueDynamically + "</a>";
    drDetails[dtDetails.Columns[1].ColumnName.ToString()] = "<a style='color:blue' href='#' onclick='return ViewBasicDetails(&quot;" + BindColumnNameValueDynamically + "," + BindColumnNameValueDynamically + "," + 1 + "&quot;);' title='Click To View'>" + BindColumnNameValueDynamically + "</a>";
}

If anyone of you know how to done this task , kindly suggest me to complete this.

Thanks and Regards,

Paul.S


Answers (1)