Click here to Skip to main content
15,912,021 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have textbox which accept the column name, one textbox for row name of csv file, and one button, on button click i want to show the specific value of row and column in other textbox,
please help me.
thank you.
sushil

What I have tried:

I am able to find only column name.
Posted
Comments
F-ES Sitecore 9-Feb-16 6:37am    
The answer depends on how you are reading the csv file. If you are reading it line by line then do a "for" loop to skip the lines you're not interested until you get to the one you are. If you're loading all lines into a string array then just access the relevant index of the array.

Update the question to include the code you have so far.

1 solution

If you can read (identify) the requested column name in the CSV file that should mean that you are already able to open a CSV file and read it in a structured fashion*. As rows aren't named or otherwise explicitly identified in a standard way, your "row name" implies that you want to find a row that has a specific value in a kind of "name" column. So you have to identify this "name" column like you did with the other requested column and then read through the rows and stop where the "name" column contains the requested row name.

* : Unless you're just doing a String.Contains(columnName) ? Then you need to use an actual solution to read CSV files. Take a look at this one: A Fast CSV Reader[^]

Edit:
Sample code using the CSVReader-solution linked above. You need to reference the "LumenWorks.Framework.IO.dll" from that solution. My test data was that small sample table from your comment in a file named "testdata.csv":
C#
// required namespaces:
using System.IO;
using LumenWorks.Framework.IO.Csv;

// ----

string nameColumnName = "a";
string valueColumnName = "d";
string rowName = "r";

using (CsvReader csvReader = new CsvReader(new StreamReader(@"..\..\testdata.csv"), hasHeaders: true))
{
    int nameColumnIndex = csvReader.GetFieldIndex(nameColumnName);
    int valueColumnIndex = csvReader.GetFieldIndex(valueColumnName);

    while (csvReader.ReadNextRecord())
    {
        if (csvReader[nameColumnIndex] == rowName)
        {
            string value = csvReader[valueColumnIndex]; // "9"

            // do something with value here

            break;
        }
    }
}
 
Share this answer
 
v2
Comments
Member 11859517 9-Feb-16 6:47am    
please give me sample, if u have.
Sascha Lefèvre 9-Feb-16 7:05am    
Please update the question to include the code you have so far. Use the green "Improve Question"-link below your question for that.
Member 11859517 9-Feb-16 7:30am    
consider an example,
csv contains,

a,b,c,d
p,1,2,3
q,4,5,6
r,7,8,9

i want to find out value of (q,c) that is 5 here,
q and c are the input of my textbox, and on buton click display the result i.e 5 in
anather textbox,

sorry i am just tring , I dont have any code,
Sascha Lefèvre 9-Feb-16 8:54am    
I'll give you a sample later (don't have the time for it right now)
Sascha Lefèvre 9-Feb-16 13:32pm    
Sample provided, please take a look.

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