Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to datatable write excel.So excel

col1,row1: Value1:

col2,row2: a1


ForExample:

Value1: MyModelEntities.Where(x=>x.ID==2).Select(x=>x.TypeID); --> a1

Value2: MyModelEntities.Where(x=>x.ID==3).Select(x=>x.TypeID); --> a2

Value3: a3

Value4: a4

Value5: a5

Value6: a6

Value7: a7

Value8: a8

What I have tried:

var a1=value1;
var a2=value2;
var a3=value3;
var a4=value4;
var a5=value5;
var a6=value6;
var a7=value7;
var a8=value8;


DataTable dt=new DataTable();
DataColumn dc=new DataColumn("col1",typeof(String));
dt.Columns.Add(dc);

dt= new DataColumn("col2",typeof(String));
dt.Columns.Add(dc);

DataRow dr=dt.NewRow();
dr[0]=("Value1:");
dr[1]=a1;
dr[2]=("Value2:");
dr[3]=a2;


Result:

value1: | a1 | value2: |a2

But I want to

value1: | a1

value2: | a2

value3: | a3

.

.

.

.

value8:| a8
Posted
Updated 15-Jan-20 9:26am
Comments
ZurdoDev 15-Jan-20 14:31pm    
What!?
Member 14169626 15-Jan-20 14:38pm    
8 rows 2 columns datatable.And set value.

You have to call NewRow for each row you want in the output: so add two items to the first row, then create a new row, and add the next to items to that.
 
Share this answer
 
Comments
Member 14169626 15-Jan-20 15:16pm    
How do I make it blank when I add a new row?I want to A2,B2 A3,B3 .... A9,B9 write.

A2:Value1:
B2:a1 (a1:5) So, Value1:5
OriginalGriff 15-Jan-20 15:43pm    
What part of "new" do you not understand?

If you fill a notebook and buy a new one, do you expect it to arrive already full of your writings?
Almost all your questions begin with "I want to ..." followed by something which gammatically shouldn't be here. This makes your questions very very hard to catch. Maybe, for future questions, you should write them in your native language and use an online translator for that? I'm not seeking to sound harsch, really, this is just to allow you to know that the quality of the answer you get is often determined by the quality of the question you ask.

For this one, you just have to create a new row for each value, instead of adding a third and fourth column which you did not even define. There also seem to be a typo with the name of the DataColumn variable.
C#
int count = 8;
string[] values = new string[count];
// Initialize values here (values[0] = "..."; values[1] = "..."; etc)

DataTable dt = new DataTable();

DataColumn dc = new DataColumn("col1", typeof(string));
dt.Columns.Add(dc);

dc = new DataColumn("col2", typeof(string));
dt.Columns.Add(dc);

for (int i = 0; i < count; ++i)
{
   DataRow dr = dt.NewRow();
   dr[0] = ($"Value{i + 1}:"); // Here you could use dr["col1"] instead
   dr[1] = values[i];          // Here you could use dr["col2"] instead
   dt.Rows.Add(dr);
}

Important points here are:

  • usage of an array to store values, instead of a single variable for each value. This makes things a lot easier.
  • a row is defined with 2 columns; do not try to stick it some more, but rather create a new row for the next value.
  • don't forget to add the new row to the collection of rows.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900