将日期转换为字符串格式

时间:2015-06-22 08:37:26

标签: vb.net converter

我已成功将数据从xls导入dataGridView。 dataGridView中的date列显示DateTime格式。我希望这个日期列显示在" dd MMM yyyy"格式。我尝试过如下所示:

connExcel.Open()
    Dim dtExcelSchema As DataTable
    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
    Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
    connExcel.Close()

    'Read Data from First Sheet
    connExcel.Open()
    cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
    oda.SelectCommand = cmdExcel
    oda.Fill(dt)
    connExcel.Close()

    dt.Columns(1).ToString("dd MMM yyyy")

    'Bind Data to GridView
    InvestorGridView.Caption = Path.GetFileName(FilePath)
    InvestorGridView.DataSource = dt
    InvestorGridView.DataBind()

但它给了我一个错误:

  

错误1重载解析失败,因为无法访问' ToString'接受这个数量的论点。 D:\ TMV \ TMV \ Project \ Investor \ Investor \ Investor.aspx.vb 68 9投资者

由于

1 个答案:

答案 0 :(得分:0)

您的方法不起作用,因为您在ToString上使用DataColumn,而DateTime没有像DataTable这样的重载。这无论如何都不起作用。 DataGridViewColumn的唯一方法是,如果您在每行中添加另一个具有适当格式的字符串列。

您应该使用InvestorGridView.Columns(1).DefaultCellStyle.Format = "dd MMM yyyy" ' DefaultCellStyle

BoundField

更新如果它是网络形式并且<asp:BoundField DataField="DateColumn" HeaderText="DateColumn-Name" SortExpression="DateColumn" DataFormatString="{0:dd MMM yyyy}" /> 您可以使用DataFormatString-property

$scope.printBillDetailsOfCaseTreatment = function (billPaymentDetails, receiptNo) {

            $scope.showBillDetailsOfCaseTreatment=true;
             var url = urllist['printSetting']+$location.search().clinicId+"/Receipt" //ClinicId

             $http.get(url,{cache:false}).success(function(data) {
                    var amountHeader = "";
                    var amount=0;
                    if(billPaymentDetails.waveOffFlag==false) {
                        amountHeader = "Payment Amount";
                        amount = billPaymentDetails.paymentAmount;
                    }
                    else {
                        amountHeader = "Waive Off Amount";
                        amount = billPaymentDetails.waveOffAmount;
                    }
                  var headerDisplay=false;
                  var clinicTime="";

                if(data!=null){
                    if(data.isHeader!=null && typeof data.isHeader!="undefined"){
                        headerDisplay=data.isHeader;
                    }
                    if(data.starttime!=null && typeof data.starttime!="undefined" && data.isClinicTiming==true){
                        clinicTime=data.starttime;
                    }
                    if(data.endtime!=null && typeof data.endtime!="undefined" && data.isClinicTiming==true){
                        clinicTime=clinicTime+" to "+data.endtime;
                    }
                }

            var printWindow = window.open('', '', 'height=400,width=800,scrollbars=yes');

            var content = '<html><head><title>Receipt</title><link rel="stylesheet" href="app/css/custom.css"><style>body {margin: 10px;padding: 20px;font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;font-size: 12px;} </style>';
            content = content + '</head><body style="margin: 10px;">';

            if(headerDisplay==true){
               var headerDiv = "";
               var headerInnerDiv = "";

                if(data.topMargin!=null && typeof data.topMargin!="undefined" && data.topMargin!=""){
                        if(data.topMargin=="0.5 inches"){
                            headerDiv = '<div class="printHeader" style="height:35px;">';
                            headerInnerDiv = '<div class="headerSpace" style="padding-top:0px;">';
                        }else if(data.topMargin=="1.0 inches"){
                            headerDiv =  '<div class="printHeader" style="height:90px;">';
                            headerInnerDiv = '<div class="headerSpace" style="padding-top:5px;">';
                        }else if(data.topMargin=="1.5 inches"){
                            headerDiv =  '<div class="printHeader" style="height:125px;">';
                            headerInnerDiv = '<div class="headerSpace" style="padding-top:20px;">';
                        }else if(data.topMargin=="2.0 inches"){
                            headerDiv =  '<div class="printHeader" style="height:160px;">';
                            headerInnerDiv = '<div class="headerSpace" style="padding-top:40px;">';
                        }else if(data.topMargin=="2.5 inches"){
                            headerDiv =  '<div class="printHeader" style="height:195px;">';
                            headerInnerDiv = '<div class="headerSpace" style="padding-top:50px;">';
                        }
                    content=content+headerDiv;  
                    content=content+headerInnerDiv; 
                }

                if(data.isLogo!=null && typeof data.isLogo!="undefined" && data.isLogo!="" && data.isLogo==true && data.logoPath!=null && typeof data.logoPath!="undefined" && data.logoPath!=""){
                    if(data.topMargin=="0.5 inches")
                    {
                        content = content + '<div class="leftHeader"><embed  src="'+data.logoPath+'" style="width: 60px; height: 35px;margin-top: 0;" /></div>';
                    }
                    else{
                        content = content + '<div class="leftHeader"><embed  src="'+data.logoPath+'" style="width: 60px; height: 40px;" /></div>';
                    }

                }

                content = content + '<div class="centerHeader">';
                    if(data.clinicName!=null && typeof data.clinicName!="undefined" && data.clinicName!="" && data.isClinicName==true){
                        if(data.topMargin=="0.5 inches")
                        {
                            content = content + '<div class="shrinkClinic">'+data.clinicName+'</div>';
                        }
                        else{
                             content = content + '<div class="clinicHead">'+data.clinicName+'</div>';
                        }
                    }
                    if(data.ownerName!=null && typeof data.ownerName!="undefined" && data.ownerName!="" && data.isDoctorName==true){
                        if(data.topMargin=="0.5 inches")
                        {
                            content = content + '<div class="shrinkDoc">'+data.ownerName+'</div>';
                        }
                        else{
                            content = content + '<div class="clinicDoc">'+data.ownerName+'</div>';
                        }
                    }
                    if(data.ownerQul!=null && typeof data.ownerQul!="undefined" && data.ownerQul!="" && data.isQualification==true){
                        if(data.topMargin=="0.5 inches")
                        {
                            content = content + '<div class="shrinkQuli">'+data.ownerQul+'</div>';
                        }
                        else{
                            content = content + '<div class="clinicQul">'+data.ownerQul+'</div>';
                        }
                    }
                content = content + '</div>';
                content = content + '<div class="rightHeader">';
                if(data.address!=null && typeof data.address!="undefined" && data.address!="" && data.isClinicAddress==true){
                    if(data.topMargin=="0.5 inches")
                    {
                        content = content + '<div class="shrinkAdd">'+data.address+'</div>';
                    }
                    else{
                        content = content + '<div class="clinicAd">'+data.address+'</div>';
                    }
                }
                if(clinicTime!=""){
                    if(data.topMargin=="0.5 inches")
                    {
                        content = content + '<div class="shrinkTime">'+clinicTime+'</div>';
                    }
                    else{
                        content = content + '<div class="clinicTim">'+clinicTime+'</div>';
                    }
                }

                content = content + '</div>';
                content = content + '</div>';
                content = content + '</div>';
            }else{
                content = content +'<div style="margin-top: 20%"></div>';  
            }


            content = content +'<div class="printBody">';   
            content = content + '<h4 style="text-align: center;"><u>Receipt</u></h4>';
            content = content + '<br><br>';


            var bodyDiv = "";
            if(data.leftMargin!=null && typeof data.leftMargin!="undefined" && data.leftMargin!=""){
                        if(data.leftMargin=="0.5 inches"){
                            bodyDiv = '<div class="pagPreview" style="padding:0 4%;">';
                        }else if(data.leftMargin=="1.0 inches"){
                            bodyDiv =  '<div class="pagPreview" style="padding:0 8%;">';
                        }else if(data.leftMargin=="1.5 inches"){
                            bodyDiv =  '<div class="pagPreview" style="padding:0 12%;">';
                        }else if(data.leftMargin=="2.0 inches"){
                            bodyDiv =  '<div class="pagPreview" style="padding:0 16%;">';
                        }else if(data.leftMargin=="2.5 inches"){
                            bodyDiv =  '<div class="pagPreview" style="padding:0 20%;">';
                        }

                    content=content+bodyDiv;    
                }


            content = content + '<hr/>';
            content = content + '<div class="leftSection">';

            if($scope.patientObj.id!=null || typeof $scope.patientObj.id!="undefined") {
                content = content + '<div>Patient Id:&nbsp;'+$scope.patientObj.id + '</div>';
            }
            content = content + '<div>Patient Name:&nbsp;'+$scope.patientObj.name + '</div>';
            if($scope.patientObj.contact!=null) {
                if($scope.patientObj.contact.mobileNumber!=null && $scope.patientObj.contact.mobileNumber!=0) {
                    content = content + '<div>Mobile No:&nbsp;'+$scope.patientObj.contact.mobileNumber + '</div>';
                }
                if($scope.patientObj.contact.contactEmail!=null && $scope.patientObj.contact.contactEmail!='') {
                    content = content + '<div>Email Id:&nbsp;'+$scope.patientObj.contact.contactEmail + '</div>';
                }
            }

            content = content + '</div>';
            content = content + '<div class="rightSection">';
            content = content + '<div>Receipt No: ' + receiptNo+ '</div>';
            content = content + '<div>Date: ' + billPaymentDetails.paymentDate+ '</div><br/>'; 
            content = content + '</div>';

            content = content + '<hr/><table CELLPADDING="2" cellspacing="25" width="100%">'; 
            content = content + '<TR style="font-size: 12px;text-align: center;">'; 
            content = content + '<TH VALIGN="TOP" BGCOLOR="#DADADA">Bill No</TH>';
            content = content + '<TH VALIGN="TOP" BGCOLOR="#DADADA">Treatment</TH>';
            content = content + '<TH VALIGN="TOP" BGCOLOR="#DADADA">Bill Amount</TH>';
            content = content + '<TH VALIGN="TOP" BGCOLOR="#DADADA">(%)Tax Applied</TH>';
            content = content + '<TH VALIGN="TOP" BGCOLOR="#DADADA">Grand Total</TH>';
            content = content + '<TH VALIGN="TOP" BGCOLOR="#DADADA">'+amountHeader+'</TH>';
            content = content + '<TH VALIGN="TOP" BGCOLOR="#DADADA">Balance Amount</TH>';
            content = content + '</TR>';
            content = content + '<tr style="font-size: 12px;text-align: center;">';
            content = content + '<td>'+billPaymentDetails.caseTreatment.caseTreatmentId+'</td>';
            content = content + '<td>'+billPaymentDetails.caseTreatment.title+'</td>';
            content = content + '<td>Rs.'+billPaymentDetails.caseTreatment.treatmentFinalBill+'/-</td>';
            content = content + '<td>'+billPaymentDetails.caseTreatment.tax.taxName+'</td>';
            content = content + '<td>Rs.'+billPaymentDetails.caseTreatment.grandTotal+'/-</td>';
            content = content + '<td>Rs.'+amount+'/-</td>';
            content = content + '<td>Rs.'+billPaymentDetails.balanceAmount+'/-</td>';
            content = content + '</tr>';
            content = content + '</table>'+'<hr/>';
            content = content + '<div>&nbsp;</div><div>&nbsp;</div>';
            content = content + '<br><div style="text-align: right;">';
            content = content + '<br>';

            content = content + '</div>';





            content = content + '<h5 style="text-align: right;font-size:12px">';
            content = content +     "Yours truly,";
            content = content + '<br><br><br><br>';
            if(data.authorisedName!=null && typeof data.authorisedName!="undefined" && data.authorisedName!=""){
                content = content + '<div>'+data.authorisedName+'</div>';
            }
            if(data.authorisedPSpl!=null && typeof data.authorisedPSpl!="undefined" && data.authorisedPSpl!=""){
                content = content + '<div>'+data.authorisedPSpl+'</div>';
            }
            if(data.authorisedQul!=null && typeof data.authorisedQul!="undefined" && data.authorisedQul!=""){
                content = content + '<div>'+data.authorisedQul+'</div>';
            }
            if(data.authorisedPhone!=null && typeof data.authorisedPhone!="undefined" && data.authorisedPhone!=""){
                content = content + '<div>'+data.authorisedPhone+'</div>';
            }


            content = content + '</h5>';
            content = content +'</div>';
            content = content +'</div>';
            var footerDiv = "";
            if(data.footerAlignment!=null && typeof data.footerAlignment!="undefined" && data.footerAlignment!=""){
                        if(data.footerAlignment==1){
                            footerDiv = '<div class="footerPreview" style="text-align:Left">';
                        }else if(data.footerAlignment==2){
                            footerDiv =  '<div class="footerPreview" style="text-align:center">';
                        }else if(data.footerAlignment==3){
                            footerDiv =  '<div class="footerPreview" style="text-align:Right">';
                        }

                    content=content+footerDiv;  
                }

            if(data.footerContent!=null && typeof data.footerContent!="undefined" && data.footerContent!="" && data.isFooter==true){
                content = content + '<div style="">'+data.footerContent+'</div>';
            }
            content = content +'</div>';

             printWindow.document.write( content + '</body></html>');   
             printWindow.document.close();
             $(".successMsgForCaseTreatmentBill").modal('hide');
             setTimeout(function(){
                 printWindow.print();
                 $scope.showBillDetailsOfCaseTreatment=false;
             }, 400);
               }); 
        };