3

我有一个 winform,它有一个 gridview,它通过存储过程填充,存储过程是

    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

我正在用这段代码动态填充gridview

    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;

我想选择 selectedRow 的 custID、contractID、totalpayment 我不知道该怎么做,第二个问题是关于我在存储过程中使用的货币符号是正确的方法还是最好的方法是什么?

4

3 回答 3

3

正如 volody 指出的那样,您可以使用 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 查询。将数据访问/业务逻辑层与表示/UI 层分离时,这通常是最佳实践。这样做的方法是DataFormatString在 DataTable 的 DataColumn 上为有问题的列指定 a。在这种特殊情况下,您可以这样做:

...
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 数据集设计器、LINQ2SQL 设计器、EntityFramework 等),以便您在设计时支持数据库查询/对象。它使在设计时将这些数据类直接绑定到 UI 控件变得更加容易,并在运行代码之前确定有关表示层的所有内容(例如将隐藏哪些列、任何额外的计算列、特殊列格式、条件行/列/单元格绘画等)。

只是我的 0.02 美元。

于 2012-05-03T18:35:32.983 回答
2

使用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);
}
于 2012-05-03T18:11:25.010 回答
0

要获取值,您将覆盖 select 事件。

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);
}

您还可以在代码中创建列...

使用 TemplateField,您可以使用以下语法将您的美元金额计算为标签或其他控件

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

但是,要走这条路,您必须设置所有字段,而不仅仅是一个。

于 2012-05-03T18:20:52.287 回答