How to get the column value of the current row of a datagridview row?

I have a winform that has a gridview that is populated with a stored procedure, a stored procedure

    ALTER PROCEDURE [dbo].[spGetAllCustumer]

    AS
    SET NOCOUNT ON 
SET XACT_ABORT ON  
    BEGIN TRAN
  BEGIN
SELECT     Customer.CustID, Customer.CustName, Customer.CompanyName, Customer.CompanyRealtion, Contract.ContractID, Contract.ContractDate, 
                  '$'+ CONVERT(varchar,Contract.totalPayment,1), '$'+ CONVERT(varchar,Contract.monthlyInstallment,1), '$'+ CONVERT(varchar,Contract.totalCommission,1), Contract.ContractDuration, Contract.PaymentMode, Contract.Description, 
                  Customer.Disable
FROM         Customer INNER JOIN
                  Contract ON Customer.CustID = Contract.CustID
WHERE     (Customer.Disable = 'false')

 END
commit

and I dynamically populate gridview with this code

    con.ConnectionString = "Data Source=MRDEVELOPER;Initial Catalog=Crystal_Accounts;Integrated Security=True";
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "spGetAllCustumer";
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Clear();
            con.Open();
            SqlDataReader dr = com.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            dataGridView1.DataSource = dt;

I want to select custID, contractID, totalpayment from the selected row, I don’t know how to do it, and the second question about the currency sign that I used in the stored procedure is the right way or the best way to do this?

+3
source share
3 answers

, SelectedRows, , . , , ( , , , ), :

foreach(DataGridViewRow row in DataGridView1.SelectedRows)
{
    // Do something with row.Cells["CustID"].Value;
    // Do something with row.Cells["ContractID"].Value;
    // Do something with row.Cells["TotalPayment"].Value;
}

, , DataGridView SQL-. , , /- / . , , DataFormatString DataColumn DataColumn . :

...
DataTable dt = new DataTable();
dt.Load(dr);
dt.Columns["TotalPayment"].DataFormatString = "C";
dt.Columns["MonthlyInstallment"].DataFormatString = "C";
dt.Columns["TotalCommission"].DataFormatString = "C";
dataGridView1.DataSource = dt;

, , , ( ). ORM (, VS DataSet Designer, LINQ2SQL, EntityFramework ..), /. , , , (, , , , /column/cell painting ..).

$0,02.

+3

SelectedRows

int columnOfcustID = 0; // change this to correct custID column index
int columnOfcontractID = 1; // change this to correct contractID column index
int columnOftotalpayment = 2; // change this to correct totalpayment column index
foreach(DataGridViewRow row in DataGridView1.SelectedRows)
{
    Console.WriteLine(row.Cells[columnOfcustID].Value);
    Console.WriteLine(row.Cells[columnOfcontractID].Value);
    Console.WriteLine(row.Cells[columnOftotalpayment].Value);
}
+2

To get the values, you must override the select event.

void GridView1_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{

   GridViewRow row = GridView1.Rows[e.NewSelectedIndex];

   //get value from cells
   String var = row.Cells[1].Text; 

   //do something with the value or pass the values to a function here
   DoSomething(var);
}

You can also create columns in your code ...

Using TemplateField, you can estimate the number of dollars in a label or other control using the following syntax

<asp:TemplateField HeaderText="Total Payment">
<itemTemplate>
    <asp:label id="lblTotalPayment" Text='<%# Eval("TotalPayment","{0:C}") %>' runat="server" />
</itemTemplate>
</asp:TemplateField>

However, in order to go through this route, you will need to configure all the fields, not just one.

0
source

All Articles