How to change the DataTable Column Name?

C#asp.netado.netDatatable

C# Problem Overview


I have one DataTable which has four columns such as

 StudentID        CourseID          SubjectCode            Marks    
------------     ----------        -------------          --------
    1               100              MT400                  80
    2               100              MT400                  79
    3               100              MT400                  88

Here I am inserting this Datatable into the Sql server table by passing this datatable as an XML Table.

I just want to Change the DataTable Column Name "Marks" as "SubjectMarks" and pass this DataTable as an XML Table.

I know how to pass the DataTable as an XML Table. But I dont know, How to change the DataTable Column Name "Marks" as "SubjectMarks".

C# Solutions


Solution 1 - C#

Try this:

dataTable.Columns["Marks"].ColumnName = "SubjectMarks";

Solution 2 - C#

Rename the Column by doing the following:

dataTable.Columns["ColumnName"].ColumnName = "newColumnName";

Solution 3 - C#

 dtTempColumn.Columns["EXCELCOLUMNS"].ColumnName = "COLUMN_NAME";                        
 dtTempColumn.AcceptChanges();

Solution 4 - C#

Use:

dt.Columns["Name"].ColumnName = "xyz";
dt.AcceptChanges();

or

dt.Columns[0].ColumnName = "xyz";
dt.AcceptChanges();

Solution 5 - C#

after generating XML you can just Replace your XML <Marks>... content here </Marks> tags with <SubjectMarks>... content here </SubjectMarks>tag. and pass updated XML to your DB.

Edit: I here explain complete process here.

Your XML Generate Like as below.

<NewDataSet>
      <StudentMarks> 
          <StudentID>1</StudentID>
          <CourseID>100</CourseID>
		  <SubjectCode>MT400</SubjectCode>
		  <Marks>80</Marks>
      </StudentMarks>
      <StudentMarks> 
          <StudentID>1</StudentID>
          <CourseID>100</CourseID>
		  <SubjectCode>MT400</SubjectCode>
		  <Marks>79</Marks>
      </StudentMarks>
      <StudentMarks> 
          <StudentID>1</StudentID>
          <CourseID>100</CourseID>
          <SubjectCode>MT400</SubjectCode>
		  <Marks>88</Marks>
      </StudentMarks>
  </NewDataSet>

Here you can assign XML to string variable like as

string strXML = DataSet.GetXML();

strXML = strXML.Replace ("<Marks>","<SubjectMarks>");
strXML = strXML.Replace ("<Marks/>","<SubjectMarks/>");

and now pass strXML To your DB. Hope it will help for you.

Solution 6 - C#

Use this

dataTable.Columns["OldColumnName"].ColumnName = "NewColumnName";

Solution 7 - C#

try this

"columns": [
{data: "id", name: "aaa", sortable: false},
{data: "userid", name: "userid", sortable: false},
{data: "group_id", name: "group_id", sortable: false},
{data: "group_name", name: "group_name", sortable: false},
{data: "group_member", name: "group_member"},
{data: "group_fee", name: "group_fee"},
{data: "dynamic_type", name: "dynamic_type"},
{data: "dynamic_id", name: "dynamic_id"},
{data: "content", name: "content", sortable: false},
{data: "images", name: "images", sortable: false},
{data: "money", name: "money"},
{data: "is_audit", name: "is_audit", sortable: false},
{data: "audited_at", name: "audited_at", sortable: false}

]

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthevanView Question on Stackoverflow
Solution 1 - C#MoonView Answer on Stackoverflow
Solution 2 - C#SaurabhView Answer on Stackoverflow
Solution 3 - C#Hardik ShahView Answer on Stackoverflow
Solution 4 - C#Subhash SainiView Answer on Stackoverflow
Solution 5 - C#Pankaj AgarwalView Answer on Stackoverflow
Solution 6 - C#Anurag DeokarView Answer on Stackoverflow
Solution 7 - C#tantengView Answer on Stackoverflow