---------送货安排在DataGrid中的显示问题-----------------
C1 C2 C3 C4 C5 C6 C7 ShouDaoTotal
------------------------------------------------------
0 100 0 20 0 10 200 125
60 50 10 0 0 60 70 110
--上面是DataTable中的数据,我要在DataGrid中显示成下面的样子---
列1 列2 列3 列4 列5 列6 列7
0 0 0 0 0 5 200
0 0 10 0 0 60 70
说明:这是一个送货安排的东西。我在一个DataTable中得到了
7天内送货的安排情况,同时也得到了已经收到了某种货物的总数ShouDaoTotal
现在要在DataGrid中显示出来 还有哪些没送!!
(请问,数据只有DataTable中的哪些数据,怎么来得到DataGrid中的显示效果呢?)
[解决办法]
没看明白,你这两项数据之间是什么关系?
[解决办法]
请表述清楚点
[解决办法]
可以在绑定之前把数据添加到数据源,如Dataset里,然后再帮定。
如:
DataRow dr = ds.Tables[0].NewRow();
for(int i=0;i <ds.Tables[0].Columns.Count;i++)
{
dr[i] = xxxxxxxx;//你的逻辑算法
}
ds.Tables[0].Rows.InsertAt(dr,ds.Tables[0].Rows.Count-1);
然后绑定。
方法有点土,但是可以用,你可以试验试验。
[解决办法]
看不懂要上面
[解决办法]
还有哪些没送!!
======
根本就搞不懂你的需求,
按你的样本数据,凭什么说 C1 C2 的数据变为 0 了?
[解决办法]
zhe这样的东西如果要绑定只能动态构造table
[解决办法]
try ->
DataTable dt = GetMyDataTable(); // 获取数据
foreach(DataRow row in dt.Rows) {
int total = (int)row[ "ShouDaoTotal "];
for(i=0; i < 7; i++) { // 假设前 7 列为目标列
int val = (int)row[i];
if(val > total) break;
//
row[i] = 0;
total -= val;
}
}
DataGrid1.DataSource = dt;
DataGrid1.Data();
[解决办法]
楼主,用PROCEDURE试试看.
PROCEDURE GetData
as
declare @c1 int
declare @c2 int
declare @c3 int
declare @c4 int
declare @c5 int
declare @c6 int
declare @c7 int
declare @ShouDaoTotal int
declare @t table(c1 int,c2 int,c3 int,c4 int,c5 int,c6 int,c7 int,ShouDaoTotal int)
declare roy cursor for select c1,c2,c3,c4,c5,c6,c7,ShouDaoTotal from count1
open roy
fetch next from roy into @c1,@c2,@c3,@c4,@c5,@c6,@c7,@ShouDaoTotal
while @@fetch_status=0
begin
if @ShouDaoTotal > @c1
begin
set @ShouDaoTotal=@ShouDaoTotal-@c1 set @c1=0
end
else
begin
set @c1=@c1-@ShouDaoTotal set @ShouDaoTotal=0
end
if @ShouDaoTotal > @c2
begin
set @ShouDaoTotal=@ShouDaoTotal-@c2 set @c2=0
end
else
begin
set @c2=@c2-@ShouDaoTotal set @ShouDaoTotal=0
end
if @ShouDaoTotal > @c3
begin
set @ShouDaoTotal=@ShouDaoTotal-@c3 set @c3=0
end
else
begin
set @c3=@c3-@ShouDaoTotal set @ShouDaoTotal=0
end
if @ShouDaoTotal > @c4
begin
set @ShouDaoTotal=@ShouDaoTotal-@c4 set @c4=0
end
else
begin
set @c4=@c4-@ShouDaoTotal set @ShouDaoTotal=0
end
if @ShouDaoTotal > @c5
begin
set @ShouDaoTotal=@ShouDaoTotal-@c5 set @c5=0
end
else
begin
set @c5=@c5-@ShouDaoTotal set @ShouDaoTotal=0
end
if @ShouDaoTotal > @c6
begin
set @ShouDaoTotal=@ShouDaoTotal-@c6 set @c6=0
end
else
begin
set @c6=@c6-@ShouDaoTotal set @ShouDaoTotal=0
end
if @ShouDaoTotal > @c7
begin
set @ShouDaoTotal=@ShouDaoTotal-@c7 set @c7=0
end
else
begin
set @c7=@c7-@ShouDaoTotal set @ShouDaoTotal=0
end
insert into @t select @c1,@c2,@c3,@c4,@c5,@c6,@c7,@ShouDaoTotal
fetch next from roy into @c1,@c2,@c3,@c4,@c5,@c6,@c7,@ShouDaoTotal
end
close roy
deallocate roy
select * from @t
其中表count1:
CREATE TABLE [count1] (
[C1] [int] NULL ,
[C2] [int] NULL ,
[C3] [int] NULL ,
[C4] [int] NULL ,
[C5] [int] NULL ,
[C6] [int] NULL ,
[C7] [int] NULL ,
[ShouDaoTotal] [int] NULL
) ON [PRIMARY]
GO
表count1的数据为:
C1 C2 C3 C4 C5 C6 C7 ShouDaoTotal
------------------------------------------------------
0100020010200125
605010006070110
楼主觉得合适的话给点分吧,哈哈.
[解决办法]
sorry, some bug to fix, plz, waiting ...
[解决办法]
foreach(DataRow row in dt.Rows) {
int total = (int)row[ "ShouDaoTotal "];
for(i=0; i < 7 && total > 0; i++) {
int val = (int)row[i];
if(val > total) {
row[i] = val - total;
total = 0;
} else {
row[i] = 0;
total -= val;
}
}
}
[解决办法]
// 精简点 ...
> > >
for(i=0; i < 7 && total > 0; i++) {
int val = (int)row[i];
row[i] = val > total ? val - total : 0;
total -= val;
}